2017-09-08 4 views
0

J'écris donc Windows update'r automat en java. Pour la récupération des données nécessaires à partir de serveurs windows j'utilise jPowerShell et j'ai stamble apon problème bizarre lors d'exécuter ce scriptDifférents comportements de script PowerShell dans différentes consoles

Java appelant 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 

si j'exécute ce script ligne par ligne dans la console standard PowerShell tout fonctionne correctement et la valeur correcte est renvoyée. Mais quand je lance ce script dans la ligne PowerShell ISE par ligne ou géré par Java je remarque un problème avec cette ligne

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

alors que j'entrer dans cette ligne et appuyez sur ENTRER je peux voir dans ISE « déjà en cours d'exécution d'une commande, s'il vous plaît attendez "quand j'attends une faw minutes communiquer est la même chose et rien ne change mais quand j'appuie sur entrer la commande de temps secound passe immédiatement. Si à partir d'eux maintenant je cours le reste du script evertying je travaille bien.

Lorsque je tente de Excecute script complet dans ISE je suis geting cette erreur

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 me donne nul de dire que je ne peux pas courir Methond sur l'objet null reffering à $ UpdateSearcher

Je suis très débutant avec PowerShell et script que j'utilise est une forme pure par exemple trouvé dans google.

+0

De quel compte le programme Java s'exécute-t-il? – Persistent13

+0

Mon compte Domein Windows 10 Édition Familiale Premium –

+0

Pouvez-vous exécuter le script avec succès sur un autre ordinateur? – Persistent13

Répondre

0

Donc, je n'avais pas compris quelle était la cause du comportement bizarre, mais j'ai réussi à écrire quelque chose qui a commencé à fonctionner pour moi via PowerShell api en Java et obtenir la valeur retournée.

$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