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.

Using PowerShell scripts without PowerShell Remoting enabled

Hi,

I am working in an environment where PowerShell Remoting is not enabled on our hosts (for security reasons - long story! emoticons_happy.png)

However, I know I can run PowerShell cmdlets locally and get remote information for cmdlets that support it and don't need Remoting. For example, the Get-WmiObject cmdlet.

Thus for example I can run something like Get-WmiObject -Class Win32_OperatingSystem -ComputerName "remotecomputer" from a PowerShell window on my Solarwinds server and it will work.

However when I put the same cmdlet in the script window of a PowerShell component in an Application Monitor it doesn't seem to do anything. I don't know how to debug further, but what I am doing is get the WMI object as above and extract the remote computer name from that and output it. If I get a computer name it means the code must be working, but I don't get anything.

I am running the script in Local Host execution mode which I believe means it runs locally on the Solarwinds server.

Any suggestions on what I am doing wrong or maybe I am trying to do something that's not supported?

Thanks.

  • I'd make sure that you have a credential defined and impersonation checked. Let us know what it looks like after you test.

    2016-08-02 14_56_54-Edit Application Template - SSL Certificate Expiration - SNI capable.png

  • Thanks Chad. I double checked and the credentials are set correctly the script is set to run under that account.

    Scr1.png

    Here's the script I am running. It's a stripped down version of what I actually want to run, but this snippet is the main bit on which everything else depends.

    $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber,CSName -ComputerName ${Node.Caption} -ErrorAction Stop

    $ComputerName = $WMI_OS.CSName

    write-host "Message: $ComputerName" ;

    write-host "Statistic: 0" ;

    exit 0 ;

    It doesn't seem to run because when I get output the "Message: " part says "N/A". I tried replacing the ${Node.Caption} variable with an actual server name itself - in case the variable passing was broken - but it returns the same.

    At the same time I can open a PowerShell window on my Solarwinds server with the credentials above and run the code snippet above (replacing the variable with a proper server name of course) and it works correctly.

    Is there any place where I can see what actually happens when the PowerShell monitor component tries to run the code?

    Regards.

  • I modified the script to output the $Error variable -

    write-host "Message: $Error" ;

    And that showed the following message - Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

    Which is odd coz I am able to run the cmdlet by open a PowerShell window on the Solarwinds server and typing it. Moreover the account it runs under has the required rights.

  • This seems like it is a permission issue on your end. I tested that script on my end and it returned just fine. I just replaced the ${Node.Caption} variable it an IP.

    2016-08-08 12_54_14-Edit Application Template - Python Script Test.png

  • Thanks Chad. But how can I troubleshoot this further?

    In the screen above I have set the script to run under a particular account. If I open a PowerShell window on my Solarwinds server as this account and run the script I get no error. So what do you think is different when the script is run by Solarwinds?

    Regards.

  • Try wrapping everything a try/catch statement. It'll output any errors to C:\ProgramData\Solarwinds\Logs\ScriptTest.log. I also removed the ; after each write-host. PowerShell doesn't require ; at the end of each line like some other scripting languages (I've run across that before)

    Try

    {

      $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber,CSName -ComputerName ${Node.Caption} -ErrorAction Stop

      $ComputerName = $WMI_OS.CSName

     

      write-host "Message: $ComputerName"

      write-host "Statistic: 0"

      exit 0

    }

    Catch

    {

        $Errormsg = $_.Exception.Message

        $Errormsg >> "C:\ProgramData\Solarwinds\Logs\ScriptTest.log"

    }

  • ...or this to place the error in the component message itself:

    catch

    {

         write-host ("Message.DiskAlert: Error: {0}" -f $_.Exception.Message);

         write-host ("Statistic.DiskAlert: -1") ;

    #This will set the component to DOWN

    exit 1;

    }

  • Just use -Credential parameter, following works for me:

    $os = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber,CSName -ComputerName ${IP} -Credential ${CREDENTIAL}

    Write-Host "Message.OS: $($os.CSName)"

    Write-Host "Statistic.OS: $($os.BuildNumber)"

    exit 0;

    No "Run the script under specified account" is required in my environment.

    This is described in help: Windows PowerShell monitor

  • Thank you Jan!  Passing the Credential solved the powershell SAM template issue that I've been working on for the past 12 hours. Thank you for sharing!