InitializationScript with -Credential

This topic contains 4 replies, has 3 voices, and was last updated by  Daniil 4 days, 19 hours ago.

  • Author
    Posts
  • #81752

    Daniil
    Participant

    I have a problem with InitializationScript functions in start-job. I'm trying to call functions in cmd-let start-job, so as not to repeat them in scriptBlock of job it's what I want. This's basically code without try and catch and others.

    $username =******
    $password =******
    $sec_password = ConvertTo-SecureString -String $password -AsPlainText -Force
    $creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $sec_password
    $functions = {
        Function GenericSqlQuery
        {
            $Datatable = New-Object System.Data.DataTable
            $Connection = New-Object System.Data.SQLClient.SQLConnection
            $Connection.ConnectionString = "Server=localhost;Trusted_Connection=True;Connection Timeout=30;"
            $connection.Open()
            $Command = New-Object System.Data.SQLClient.SQLCommand
            $Command.Connection = $Connection
            $Command.CommandText = "Select @@servername as name" 
            $DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $Command
            $DataAdapter.SelectCommand.CommandTimeout = 600
            $Dataset = new-object System.Data.Dataset
            [void]$DataAdapter.Fill($Dataset)
            return $Dataset.tables[0]
        }
    }
    [Scriptblock]$Scriptblock = {
        GenericSqlQuery
    }
    Start-Job -InitializationScript $functions -ScriptBlock $Scriptblock -Credential $creds | Wait-Job | Receive-Job
    
     CategoryInfo          : OpenError: (localhost:String) [], PSRemotingTransportException
        + FullyQualifiedErrorId : -2147467259,PSSessionStateBroken
    error: [localhost] An error occurred while starting the background process. Error reported: The stub received bad data.
    

    I know you say your credentials are corrupt but my code works without -InitializationScript with $creds and no problem.

    What am I doing wrong?(((

  • #81835

    Cyril Muffat
    Participant
    $username ="yourusername"
    $password ="yourpassword"
    $sec_password = ConvertTo-SecureString -String $password -AsPlainText -Force
    $creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $sec_password
    
    $functions = {
        Function GenericSqlQuery
        {
            Write-host "i ve just killed a puppy"
        }
    }
    [Scriptblock]$Scriptblock = {
        
        GenericSqlQuery
    }
    Start-Job  -ScriptBlock $Scriptblock -InitializationScript $functions -Credential $creds | Wait-Job | Receive-Job
    

    that part should work fine, either ur query is not working, or $password is not in plaintext
    test ur $cred with something like this : $S = New-pssession -computername . -credential $cred

    • #82108

      Daniil
      Participant

      You can run like this.
      Connection.open()
      It does mean that connection opens with creds.
      The problem isn't in $creds.

      $username = "******"
      $password = "******"
      $sec_password = ConvertTo-SecureString -String $password -AsPlainText -Force
      $creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $sec_password
      
      $functions = {
          Function GenericSqlQuery
          {
              $Datatable = New-Object System.Data.DataTable
              $Connection = New-Object System.Data.SQLClient.SQLConnection
              $Connection.ConnectionString = "Server=localhost;Trusted_Connection=True;Connection Timeout=30;"
              $connection.Open()
          }
      }
      [Scriptblock]$Scriptblock = {
          GenericSqlQuery
      }
      Start-Job -InitializationScript $functions -ScriptBlock $Scriptblock -Credential $creds | Wait-Job | Receive-Job
      
  • #81874

    postanote
    Participant

    Also, doing this...

    $username ="yourusername"
    $password ="yourpassword"

    $sec_password = ConvertTo-SecureString -String $password -AsPlainText -Force
    $creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $sec_password

    ... in scripts is really a bad idea.

    You can simply do this... as this one line replaces the above 4 lines a obviously more secure way to doing this.

    # Ask for the user credentials using the system dialog box.
    $creds = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME"

    If for any reason you 'd need to use this credential separately, in most case you shouldn't have to, you'd just do this..

    $creds.UserName
    $creds.Password

    # So, your block would be...

    $creds = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME"

    $functions = {
    Function GenericSqlQuery
    {
    Write-host "i ve just killed a puppy"
    }
    }
    [Scriptblock]$Scriptblock = {

    GenericSqlQuery
    }
    Start-Job -ScriptBlock $Scriptblock -InitializationScript $functions -Credential $creds | Wait-Job | Receive-Job

    • #81941

      Daniil
      Participant

      You know, like this it works.
      But It wasn't exactly what I wanted. The problem isn't in $creds.
      You can test it. No one can help.

      $username =******
      $password =******
      $sec_password = ConvertTo-SecureString -String $password -AsPlainText -Force
      $creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $username, $sec_password
      
      [Scriptblock]$Scriptblock = {
          Function GenericSqlQuery
          {
              $Datatable = New-Object System.Data.DataTable
              $Connection = New-Object System.Data.SQLClient.SQLConnection
              $Connection.ConnectionString = "Server=localhost;Trusted_Connection=True;Connection Timeout=30;"
              $connection.Open()
              $Command = New-Object System.Data.SQLClient.SQLCommand
              $Command.Connection = $Connection
              $Command.CommandText = "Select @@servername as name" 
              $DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $Command
              $DataAdapter.SelectCommand.CommandTimeout = 600
              $Dataset = new-object System.Data.Dataset
              [void]$DataAdapter.Fill($Dataset)
              return $Dataset.tables[0]
          }
          GenericSqlQuery
      }
      Start-Job -ScriptBlock $Scriptblock -Credential $creds | Wait-Job | Receive-Job
      
      

You must be logged in to reply to this topic.