Different PowerShell script behavior in different console

671 views Asked by At

So i write Windows update'r automat in java. For fetching needed data from windows servers i am using jPowerShell and i have stamble apon weird problem while execute this script

Java calling PowerShell Script

PowerShell ps = PowerShell.openSession();
        PowerShellResponse response;
        response = ps.executeScript("C:\\Users\\Prezes\\Desktop\\IsUpdateToInstal.ps1");
        System.out.println(response.getCommandOutput());

PowerShell Script

$pw = ConvertTo-SecureString 'password' -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentList domein\login, $pw
Enter-PSSession -ComputerName IP -Credential $cred
$UpdateSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session")) 
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()             
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
            $Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
                $important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
                $other = $SearchResult.updates | where { $_.MsrcSeverity -eq $nul}
                $totalUpdates = $($SearchResult.updates.count)
                if($totalUpdates -gt 0)
                {
                    $updatesToInstall = $true
                }
                else { $updatesToInstall = $false }
$other
$totalUpdates
$updatesToInstall

if i execute this script line by line in PowerShell standard consol everyting is working fine and proper value are returned. But when i run this script in PowerShell ISE line by line or run by Java i notice some problem with this line

$UpdateSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session")) 

while i enter this line and press enter i can see in ISE "already running a command, please wait" when i wait a faw minutes communicate is the same and nothing change but when i press enter secound time command pass through immediately. If from them now i run rest of script evertying i working well.

When i try to excecute full script in ISE i am geting this error

    Exception form HRESULT: 0x80072EE2
At C:\Users\Prezes\Desktop\IsUpdateToInstal.ps1:6 char:1
+ $SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMExceptio

Java gives me null saying that i can't run methond on null object reffering to $UpdateSearcher

I am very early beginer with PowerShell and script that i am using is pure form some example finded in google.

1

There are 1 answers

0
Adrian On

So i had not figure out what was the cause of weird behavior but i managed to write someting that started to work for me through PowerShell api in java and get returned value.

    $pw = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
   $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentList 792\opmanager, $pw
$TotalUpdates = Invoke-Command -ComputerName IP -ScriptBlock{
$UpdateSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session")) 
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()             
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
            $Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
                $important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
                $other = $SearchResult.updates | where { $_.MsrcSeverity -eq $nul}
                $totalUpdates = $($SearchResult.updates.count)
                if($totalUpdates -gt 0)
                {
                    $updatesToInstall = $true
                }
                else { $updatesToInstall = $false }
Return $totalUpdates
} -Credential $cred
$TotalUpdates