Issues with a Script i have to find and script out SQL replication.

The error i am getting is:
[pre]Exception calling "LoadWithPartialName" with "1" argument(s): "Could not load file or assembly 'Microsoft.SqlServer.Replication, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file 
specified."
At C:\blah\blah\blah\Powershell_scripts\Replication3.ps1:18 char:1
+ [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Repli ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException [/pre]

 

My actual code is as follows. The only thing i have change is server names and paths.

#The PowerShell script is below, simply save it to a file called ScriptReplication.ps1;

#Load command-line parameters - if they exist
#param ([string]$sqlserver, [string]$filename)
param (
[string]$Subscribers = @('SQL12','SQL20','SQL22','SQL3'),
[string]$filename)

#Reference RMO Assembly
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Replication") | out-null
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Rmo") | out-null

#function errorhandler([string]$errormsg)
#{
# writetofile ("-- Replication Script Generator run at: " + (date)) $filename 1
# writetofile ("-- [Replication Script ERROR] " + $errormsg) $filename 0
#}

function writetofile([string]$text, [string]$myfilename, [int]$cr_prefix)
{
if ($cr_prefix -eq 1) { "" >> $myfilename }
$text >> $myfilename
}

function initializefile([string]$myfilename)
{
"" > $myfilename
}

#trap {errorhandler($_); Break}

#Deal with absent parameters
[string] $hostname=hostname
if ($sqlserver -eq "") {$sqlserver = read-host -prompt "Please enter the server name or leave blank for Hostname"}
if ($filename -eq "") {$filename = read-host -prompt "Please enter the file name (eg 'c:\ReplicationBackupScript.sql')..."}
if ($sqlserver -eq "") {$sqlserver = $hostname}
if ($filename -eq "") {$filename = "c:\blah\blah\blah\Powershell_scripts\output\ReplicationBackupScript.sql"}

# Clear file contents
if (Test-Path ($filename)) {Clear-Content $filename}

$repsvr=New-Object "Microsoft.SqlServer.Replication.ReplicationServer" $sqlserver

initializefile $filename

# if we don't have any replicated databases then there's no point in carrying on
if ($repsvr.ReplicationDatabases.Count -eq 0)
{
writetofile ("-- Replication Script Generator run at: " + (date)) $filename 0
writetofile "-- ZERO replicated databases on $sqlserver!!!" $filename 1
EXIT
}

# similarly, if we don't have any publications then there's no point in carrying on
[int] $Count_Tran_Pub = 0
[int] $Count_Merge_Pub = 0

foreach($replicateddatabase in $repsvr.ReplicationDatabases)
{
$Count_Tran_Pub = $Count_Tran_Pub + $replicateddatabase.TransPublications.Count
$Count_Merge_Pub = $Count_Merge_Pub + $replicateddatabase.MergePublications.Count
}

if (($Count_Tran_Pub + $Count_Merge_Pub) -eq 0)
{
writetofile ("-- Replication Script Generator run at: " + (date)) $filename 0
writetofile "-- ZERO Publications on $sqlserver!!!" $filename 1
EXIT
}

# if we got this far we know that there are some publications so we'll script them out
# the $scriptargs controls exactly what the script contains
# for a full list of the $scriptargs see the end of this script
$scriptargs =[Microsoft.SqlServer.Replication.scriptoptions]::Creation `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeArticles `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublisherSideSubscriptions `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeSubscriberSideSubscriptions

writetofile ("-- Replication Script Generator run at: " + (date)) $filename 0
writetofile "-- PUBLICATIONS ON $sqlserver" $filename 1
writetofile "-- TRANSACTIONAL PUBLICATIONS ($Count_Tran_Pub)" $filename 1

foreach($replicateddatabase in $repsvr.ReplicationDatabases)
{
if ($replicateddatabase.TransPublications.Count -gt 0)
{
foreach($tranpub in $replicateddatabase.TransPublications)
{
writetofile "/********************************************************************************" $filename 0
writetofile ("***** Writing to file script for publication: " + $tranpub.Name) $filename 0
writetofile "********************************************************************************/" $filename 0
[string] $myscript=$tranpub.script($scriptargs) 
writetofile $myscript $filename 0
}
}
}

writetofile "-- MERGE PUBLICATIONS ($Count_Merge_Pub)" $filename 1

foreach($replicateddatabase in $repsvr.ReplicationDatabases)
{
if ($replicateddatabase.MergePublications.Count -gt 0)
{
foreach($mergepub in $replicateddatabase.MergePublications)
{
writetofile "/********************************************************************************" $filename 0
writetofile ("***** Writing to file script for publication: " + $mergepub.Name) $filename 0
writetofile "********************************************************************************/" $filename 0
[string] $myscript=$mergepub.script($scriptargs) 
writetofile $myscript $filename 0
}
}
}

Are you doing this on a SQL host directly or from a remote host, via PSRemoting?

If you have not SQL on you admin workstation, this is supposed to fail. You to have those libraries on your system and registered before they are useable.

If you have not loaded SQLPS, things will not be available.
Example with a video demo that is in the realm of your use case.

https://pwsh.wordpress.com/2013/03/31/replication-monitor-powershell
https://www.mssqltips.com/sqlservertip/4145/using-powershell-for-sql-server-replication-tracking

You also have not said what version of SQL you are using.
Here’s why I make this statement.

Note There are two SQL Server PowerShell modules:

SQLPS:
This module is included with the SQL Server installation (for backwards compatibility), but is no longer being updated. The most up-to-date PowerShell module is the SqlServer module.

SqlServer:
This module includes new cmdlets to support the latest SQL features. The module also contains updated versions of the cmdlets in SQLPS.
https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module?view=sql-server-2017

Also consider your.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft...

… to this …

Add-Type -AssemblyName "Microsoft...

Why…
Modules in PowerShell can have manifests, which essentially are used to customize the environment or load types that the module needs to function to load the assemblies you need.