Executing program on remote computer help

Hello and thank you for your time. Here is what I’m trying to do and the requirements behind it.

PowerShell 2.0

Must execute on remote computer

Script so far:

$RemoteDll = “c:\program files (x86)\test\location\application.dll”
invoke-command -computername ‘test-pc01’ -ScriptBlock { & ‘regsvr32’ $RemoteDll}

The above command does not work to register application.dll.

In addition to registering the DLL I need to unregister it. Therefore, I need to use the /u switch along with the /s switch for silent.

Where am I going wrong?

The script block you pass to Invoke-Command does not inherit any variables from your local scope. In this case, $RemoteDll is an uninitialized (null) value when the command runs on the remote computer. You need to pass the value as an argument, and write the script block to accept positional parameters (either by name, or in the automatic $args array). For example:

$RemoteDll = "c:\program files (x86)\test\location\application.dll"

invoke-command -computername 'test-pc01' -ScriptBlock { & 'regsvr32' $args[0] } -ArgumentList $RemoteDll

Thanks Dave that did the trick!