Start-Job Credential

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #6047

    by smadon at 2013-03-05 11:46:28

    Hi,

    I have a small command which I don't get the error


    $Username = "Admin"
    $Password = "MyPasswords"
    $global:Credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($Username,(ConvertTo-SecureString -String $Password -AsPlainText -Force))

    $MyScript = { Set-ItemProperty -path $RegKey -name repository -value $GoodRep }
    $Job = Start-Job -ScriptBlock $MyScript -Credential $Credential
    Wait-Job $Job
    Receive-Job $job


    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. Error reported:The directory name is invalid.
    At D:\Users\Myuser\AppData\Local\Temp\Quest Software\PowerGUI\XXXXXX\RunMyProg.ps1:62 char:14
    + Receive-Job < <<< $job
    + CategoryInfo : OpenError: (:) [Receive-Job], PSRemotingTranspor
    tException
    + FullyQualifiedErrorId : PSSessionStateBroken

    What does is mean "The directory name is invalid." :-/

    Thanks for your help

    by mjolinor at 2013-03-05 12:02:46

    I believe it means that it's not happy that the argument to the -Path parameter is nulll.

    $MyScript = { Set-ItemProperty -path $RegKey -name repository -value $GoodRep }

    That $RegKey variable is not going to get expanded from a local variable before the script block is sent to the background job. Since the variable doesn't exist in that session it's going to resolve to null.

    You can create a script block from local variables using [scriptblock]::create() and an expandable string:

    $MyScript = [scriptblock]::create(" Set-ItemProperty -path $RegKey -name repository -value $GoodRep")

    or you can pass values for those variables using the -ArgumentList parameter of Start-Job

    See get-help start-job -full

    by smadon at 2013-03-05 13:19:15

    Thanks for the quick reply, but euhhh... sorry I did not understand everything :-/

    And my mistake,
    I forget to put the variable (I declare it before) in the code

    So the full

    $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))
    {
    $MyScript = { Set-ItemProperty -path $RegKey -name repository -value $GoodRep }
    $Job = Start-Job -ScriptBlock $MyScript -Credential $Credential
    Wait-Job $Job
    Receive-Job $job
    }

    If I success to do a get-ItemProperty why I could not do a Set-ItemProperty and have a Directory Folder error ?????

    by mjolinor at 2013-03-05 13:43:48

    Get-ItemPrpoperty is being run in the local session, where the $RegKey variable exists, so it works there.

    Start-Job creates a new session and attempts to execute the script block in that session. The $RegKey variable does not exist in that remote session, so it fails.

    by smadon at 2013-03-05 15:16:44

    I success to pass the parameter but I still the error message when I put the credential option

    $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

    Still don't get how I can start the job with Credential ?????
    Thanks again for your help

    by mjolinor at 2013-03-05 17:33:18

    Do you get a different error now?

    by smadon at 2013-03-05 22:46:29

    If I don't have credential option, I have the message for Registry access is not allowed, which for me seems normal because I run it with a user who don't have admin right.


    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 Completed True localhost
    RunspaceId : 07XXX152-XXXe-4XXX-bXX4-dcXX0601aXXX
    UserName : Admin
    Password : System.Security.SecureString
    HKLM:\Software\MyProgr\MyProg\Reps\1
    Requested registry access is not allowed.
    + CategoryInfo : PermissionDenied: (HKEY_LOCAL_MACH...d\MyProg\Reps\1:String) [Set-ItemProperty], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.SetItemPropertyCommand

    And If I had Credential Option, I have the same error message as before

    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

    by mjolinor at 2013-03-06 03:52:19

    Does the command run with those credentials in your current session (not as a background job)?

    by smadon at 2013-03-06 03:56:42

    I suppose yes, it's running in the current session.

    by smadon at 2013-03-06 14:30:31

    Hi guys,

    Anybody have an idea for this error ?? :-/

    Thanks for your help.
    Smadon

    by miky at 2013-04-04 09:36:43

    Have you tried these links?

    http://www.alexwinner.com/articles/powershell/62-startjob-error-directory-name-invalid.html

    http://support.microsoft.com/kb/832434

    by MasterOfTheHat at 2013-04-05 08:38:40

    Give us some more details...
    What have you put into the $Arguments variable used here?
    $Job = Start-Job -ScriptBlock $MyScript -ArgumentList $Arguments -Credential $Credential
    Have you tried using the Get-Credential cmdlet to see if there is a problem with where you're creating the $Credential variable?
    $Credential = Get-Credential
    Also, you are creating the $Rep variable based on the local machine, but you're using it to determine if you should modify the remote machine's registry. Shouldn't you be checking for that path on the remote machine, too?

You must be logged in to reply to this topic.