0
votes

What used to work in my script doesn't anymore and I am out of ideas.

I'm trying to do the following:

Import-Module 'H:\folder\folder1\Remove-Software.psm1'
$sessionoption = New-PSSessionOption -SkipCACheck -SkipCNCheck
$s = New-PSSession -ComputerName $Global:DevicePrimaryIP -Credential $Global:Creds -SessionOption $sessionoption
Invoke-Command -Session $s -ScriptBlock $function:RemoveSoftware

This should log into the remote software and remove software, however I keep receiving the following error:

Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The
argument is null. Provide a valid value for the argument, and then try running
the command again.
At line:1 char:41

Also, I've tried every combination for the scriptblock I can think of.

I've changed things elsewhere in my script but haven't touched this for some time. If anyone else can see the problem anyhelp would be appreciated.

1
You don't seem to have a function RemoveSoftware in your script. - Ansgar Wiechers
The function is imported from another module - Mike
Within that module it contains a function called removesoftware (No hyphen) several processes and directories that are killed/deleted. No arguments are parsed to it either, just to run a small snippet of code on the remote server: function removesoftware{ kill process delete directory return "software has been removed" } - Mike
What happends if you run $function:removesoftware alone? Just to see that the function is defined - Frode F.
I believe this is where my problem is.. It doesnt actually do anything, also autocomplete doesnt see it. I must be missing something, it has been imported, I can browse to the directory where this module is located too. - Mike

1 Answers

0
votes

Your function is not imported from the module. You can use -Verbose to see what's imported. Ex:

Import-Module .\Desktop\testmodule.psm1 -Verbose
VERBOSE: Loading module from path 'C:\Users\frode\Desktop\testmodule.psm1'.
VERBOSE: Exporting function 'testModuleFunction'.
VERBOSE: Importing function 'testModuleFunction'.

If the "module" is just a script-file with a few functions you could try to simply dot-source them in:

. 'H:\folder\folder1\Remove-Software.psm1'