Question Start-Job with Credential

Plus d'informations
il y a 13 ans 2 mois #14247 par smadon
Start-Job with Credential a été créé par smadon
Hello,

Je rencontre un problème avec start-job que je voudrais lancer avec un credential car je voudrais modifier une clé de registre.

Mais quand je lance mon script, je reçois un message : \"Directory Invalid\"

Je ne comprends pas ou est l'erreur.


[code:1]
$Username = \"Admin\"
$Password = \"MyPassword\"
$global:Credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($Username,(ConvertTo-SecureString -String $Password -AsPlainText -Force))
$RegKey = \"HKLM:\Software\MySoft\MySoft\Reps\1\"
$GoodRep = \"http://MYSERVER/Rep.xml\"
# Read the Registry
$Rep = Get-ItemProperty -path $RegKey

# If not the Good Rep, we write the good name
if (!($Rep.repository -eq $GoodRep))
{
Write-Output \"Rep not Correct, we change it.\"
$MyScript = { param ( $RegKey, $GoodRep)
Write-Output $Credential
Write-Output $RegKey
Set-ItemProperty -path $RegKey -name repository -value $GoodRep
}
$Job = Start-Job -ScriptBlock $MyScript -ArgumentList $Arguments -Credential $Credential
Wait-Job $Job
Receive-Job $job
}

[/code:1]


Le Message d'Erreur :
[code:1]
D:\>MyProg.exe
Rep not Correct, we change it.

WARNING: column \"Command\" does not fit into the display and was removed.

Id Name State HasMoreData Location
-- ----


1 Job1 Failed False localhost
Receive-Job : [localhost] There is an error launching the background process. E
rror reported: The directory name is invalid.
At D:\Users\user\AppData\Local\Temp\Quest Software\PowerGUI\XXXXX\RunMyProg.ps1:68 char:14
+ Receive-Job <<<< $job
+ CategoryInfo : OpenError: (:«») [Receive-Job], PSRemotingTransportException
+ FullyQualifiedErrorId : PSSessionStateBroken
[/code:1]


D'avance merci pour votre aide

Damien

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 2 mois #14260 par Arnaud Petitjean
Bonjour Damien,

Je ne vois pas à quel moment tu passes tes arguments :
[code:1]$Job = Start-Job -ScriptBlock $MyScript -ArgumentList $Arguments -Credential $Credential[/code:1]

Autrement dit ta variable $Arguments n'est pas définie dans ton script.

Est-ce que tu récupères bien des valeurs lorsque dans ton scriptblock sont executés les 2 Write-Output ?

Arnaud

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.041 secondes
Propulsé par Kunena