Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 10

Powershell Wrapper Script


I monitor a lot with the powershell script monitor components. Some times I have the same powershell script in an application more than 10 times. So if I want to change something in the script I have to go to the template and change the same lines in each of the component.

Therefore I thought I build an wrapper, that would make it possible to just change the lines at the script locally.

At this time my wrapper looks like this:


My first parameter is the path to the script that should be run (called script). All other parameters will be forwarded to the script defined in the first parameter.

For test purposes I wrote this test script


If I run the wrapper script locally I get this output:



But I want to run this in SAM, I get no output. So I added some output in the wrapper script in SAM:


It seems that just the output of the wrapper script is used for statistic and messages.


Do you have any idea to get the output of the called script?

If you have any improvments please tell me 🙂







0 Kudos
2 Replies

Re: Powershell Wrapper Script

I would use 'dot sourcing' something like the code below will add in files as if they were in the script body themselves. I don't have a minute to try it in SAM and give you exact code right now but if you run into difficulties please @ me and I will see what I can do. Once you dot source a file it just like its in the script directly. Its great for re-used functions (assuming that you keep the outputs the same)

if (Test-Path -Path 'C:\Location\file.ps1') {
. 'C:\Location\file.ps1'
elseif (Test-Path -Path "$PSScriptRoot\file.ps1") {
. "$PSScriptRoot\file.ps1"
else {
Write-Warning -Message 'file.ps1 not found'

Later if you want to get really fancy, you can do something similar and pull it in from a git repository.

0 Kudos
Level 10

Re: Powershell Wrapper Script

@jm_sysadmin I tried this but another problem occurred. If I use dot sourcing all parameters are passed as an array to the called script. This means all passed parameters are stored in an array in $args[0]. So if I want to access the agruments in the called script I have to access them like $args[0][0] or $args[0][1] and so on.


0 Kudos