This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

How to pass in parameter to PS script

I have a PS script that will delete msi files that are older than 1 year. I set up an alert to trigger if there's less than 2gb of space left on the C:\ drive. I want this script to execute against the triggered node. How do i pass in the node that was triggered to the script?

Here's my code:

$server = '${IP}'

$DeleteOlderThan = (Get-Date).AddDays(-365) #files older than a year

$Destination = "\\$server\C$\Windows\Installer\"

Get-ChildItem $Destination -File | Where-Object { $_.LastWriteTime -lt $DeleteOlderThan } | Remove-Item -Include "*.msi" -Force

And this is my Network Path to external program:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -Command "C:\PROGRAMNAME.ps1 '${IP}'"

It seems the ${IP} is not passing in at all.

Any help would be greatly appreciated!

  • Your code needs to be something like this:

    $server = $args[0]

    inside powershell it doesn't know what ${IP} is supposed to be, but it does know what an incoming argument is.

  • I thought it would get ${IP} from the Network Path to external program (under trigger actions where you edit the alert): C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -Command "C:\PROGRAMNAME.ps1 '${IP}'"

  • mesverrum​ I got this working and can pass in my own variable:

    $server = $args[0]

    $DeleteOlderThan = (Get-Date).AddDays(-365) #files older than a year

    $Destination = "\\$server\C$\Windows\Installer\"

    Get-ChildItem $Destination -File | Where-Object { $_.LastWriteTime -lt $DeleteOlderThan } | Remove-Item -Include "*.msi" -Force

    But i am unable to pass in the variable from he Network Path to external program (under trigger actions where you edit the alert):

    C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -Command "C:\StoreVmCleanup.ps1 ${N=SwisEntity;M=Node.Caption}"

    Any ideas?