registry changes

Welcome Forums General PowerShell Q&A registry changes

Viewing 6 reply threads
  • Author
    Posts
    • #210000
      Participant
      Topics: 1
      Replies: 4
      Points: 2
      Rank: Member

      I am in need of serious help! I have been working my hardest on trying to create a PS script that will set-itemproperty for each computer that is listed in a CSV with headers and I have been failing miserably. I have tried to run my code using foreach loops, assigned items to collectionObjects and I have yet to get what I need. I came close with the collectionObjects variable but noticed at the end that it will only set constant values in the csv and not different values per computer. Please don’t bash me as I am still learning and not as advanced as I would like to be.

      headers consist of { Status, Name, Hardware, Scope, Duration..) all saved in a .csv file.

      #each collectionobject is assigned a header. The goal is to list a set of computers in the .csv or another file(which ever is better) and have the csv file append the changes that are present in each row.

      # than Invoke-Command -ComputerName $computers -Credential $credential -ScriptBlock {Set-ItemProperty -Path “HKLM:\SYSTEM\path\status  -Name “Status  -Value $collectionObject.status }

       

      Name of Computer Status Hardware Scope Duration
      Tom Active present full indefinite
      Amber delayed installed full 30 days

       

      $collectionObject[0].Status
      $collectionObject[0].Name
      $collectionObject[0].Hardware
      $collectionObject[0].Scope
      $collectionObject[0].Duration

       

      $collectionObject = Import-Csv -Path .\Desktop\RegistryFields.csv
      $servers = “tom”, “amber”
      $credential = Get-Credential -Credential #add creds here

      Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {Set-ItemProperty -Path “HKLM:\SYSTEM\path\Status” -Name “status” -Value $collectionObject.status}

    • #210015
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Please, when you post code or sample data or console output format it as code using the code tags “PRE“. Thanks.

      Something like this should get you started:

      $inputData = @'
      Name of Computer,Status,Hardware,Scope,Duration
      Tom,Active,present,full,indefinite
      Amber,delayed,installed,full,30 days
      '@ |
      ConvertFrom-Csv
      
      $credential = Get-Credential -Credential #add creds here
      
      Foreach ($element in $inputData) {
          Invoke-Command -ComputerName $element.'Name of Computer' -Credential $credential -ScriptBlock { Set-ItemProperty -Path 'HKLM:\SYSTEM\path\status'  -Name 'Status'  -Value $USING:element.Status }
      }
      • #210108
        Participant
        Topics: 1
        Replies: 4
        Points: 2
        Rank: Member

        Thank you for the assistance but I have two items that are not presenting correctly:

        1. inputdata works great if the options under each header would be constant but they wont be, is there anyway that the data can be parsed from a csv so that all changes are made at the csv?
        2. Invoke-Command : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Provide an argument that is not null or empty, and then
          try the command again. (this is what I get when I fill in the ncessary items.  #Invoke-Command -ComputerName $element.tom will not read
    • #210018
      Participant
      Topics: 5
      Replies: 322
      Points: 446
      Helping Hand
      Rank: Contributor
      $csv = Import-Csv -Path .\Desktop\RegistryFields.csv
      $server = “tom”, “amber”
      $credential = Get-Credential -Credential #add creds here
      
      #Use csv in remote session and change registry entry
      Invoke-Command -ComputerName $server -ScriptBlock {
          Param($rcsv) $obj = $rcsv | Where-Object {$_.Name -eq $env:COMPUTERNAME} ; 
          Set-ItemProperty -Path “HKLM:\SYSTEM\path\Status” -Name Status -Value $obj.Status
      } -ArgumentList (,$csv) -Credential $credential
      
      • #210123
        Participant
        Topics: 1
        Replies: 4
        Points: 2
        Rank: Member

        Thank you for the assistance but when I run the script with the necessary changes it doesn’t read the -value switch nor does the informaton parse from the csv using the $obj.SystemDescription? Thoughts?

    • #210129
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      … I have two items that are not presenting correctly

      …not presenting correctly” ??? What does that mean? Please have in mind: We cannot see your screen and we cannot read your mind. Please be as detailed as possible when describing your situation before and what you try to achieve.

      inputdata works great if the options under each header would be constant but they wont be, is there anyway that the data can be parsed from a csv so that all changes are made at the csv?

      ????

      Invoke-Command : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Provide an argument that is not null or empty, and then

      try the command again. (this is what I get when I fill in the ncessary items. #Invoke-Command -ComputerName $element.tom will not read

      If the property you get from the CSV data in the column “Name of Computer” does not exist in your DNS or is not available at the moment the command will fail.

    • #210225
      Participant
      Topics: 1
      Replies: 4
      Points: 2
      Rank: Member

      Thank you, I am going to get this right if it kills me. I have included a complete script I am working on as I am doing it without substitutions

      $sScriptVersion = “1.0” $Creator = ‘myself’ $TimeStamp = (Get-Date).ToString(‘yyyy-MM-dd HH:mm:ss’) $server = $server.Count $collectionObject = Import-Csv -Path .\Desktop\RegistryFields.csv $server = “tom”, “amber” Write-Host “********************************** Script $sScriptVersion Started $Timestamp written by $creator *********************************************” -foregroundcolor Green #these are the values stored in the collectionObjects $collectionObject[1…20].ATOStatus $collectionObject[1…20].Network $collectionObject[1…20].PatchPhase $collectionObject[1…20].ProjectEstimatedEndDate $collectionObject[1…20].ProjectID $collectionObject[1…20].ProjectManagerEmail $collectionObject[1…20].ProjectName $collectionObject[1…20].ProjectPPDSponsor $collectionObject[1…20].PurposeOrApplication $collectionObject[1…20].SystemDescription foreach($server in $collectionObject) { Write-Output “—————————Setting Registry Changes with $server servers for modification————————–” Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ATOStatus” -Name “ATOStatus” -Value $collectionObject.ATOStatus -WhatIf -verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\Network” -Name “Network” -Value $collectionObject.Network -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\PatchPhase” -Name “PatchPhase” -Value $collectionObject.PatchPhase -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ProjectEstimatedEndDate” -Name “ProjectEstimatedEndDate” -Value $collectionObject.ProjectEstimatedEndDate -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ProjectID” -Name “ProjectID” -Value $collectionObject.ProjectID -whatif -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ProjectManagerEmail” -Name “ProjectManagerEmail” -Value $collectionObject.ProjectManager -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ProjectName” -Name “ProjectName” -Value $collectionObject.ProjectName -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\ProjectPPDSponsor” -Name “ProjectPPDSponsor” -Value $collectionObject.ProjectPPDSponsor -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\PurposeOrApplication” -Name “PurposeOrApplication” -Value $collectionObject.PurposeOrApplication -WhatIf -Verbose} Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path “HKLM:\SYSTEM\path\SystemDescription” -Name “SystemDescription” -Value $collectionObject.SystemDescription -WhatIf -Verbose } }

       

      Errors:

      Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of
      strings.

    • #210231
      Participant
      Topics: 1
      Replies: 4
      Points: 2
      Rank: Member

      This is the error code I am getting: “Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.”

      Again, All I am doing is parsing information from the csv which has the headers attached which are being called by the $collectionObject and than I am set-itempropety for existing fields in the registry with the information from the csv.

      $sScriptVersion = "1.0"
      $Creator = 'mysef'
      $TimeStamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
      $Server = $server.Count
      $collectionObject = Import-Csv -Path .\Desktop\RegistryFields.csv
      $server = “tom”, “amber” 
      Write-Host "********************************** Script $sScriptVersion Started $Timestamp written by $creator  *********************************************" -foregroundcolor Green
       
      #these are the values stored in the collectionObjects
          $collectionObject[1...20].ATOStatus
          $collectionObject[1...20].Network
          $collectionObject[1...20].PatchPhase
          $collectionObject[1...20].ProjectEstimatedEndDate
          $collectionObject[1...20].ProjectID
          $collectionObject[1...20].ProjectManagerEmail
          $collectionObject[1...20].ProjectName
          $collectionObject[1...20].ProjectPPDSponsor
          $collectionObject[1...20].PurposeOrApplication
          $collectionObject[1...20].SystemDescription
      
      foreach($server in $collectionObject) {
      
      
      
           Write-Output "---------------------------Setting Registry Changes with $server servers for modification--------------------------"  
      
      
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ATOStatus" -Name "ATOStatus" -Value $collectionObject.ATOStatus -WhatIf -verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\Network" -Name "Network" -Value $collectionObject.Network -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\PatchPhase" -Name "PatchPhase" -Value $collectionObject.PatchPhase -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ProjectEstimatedEndDate" -Name "ProjectEstimatedEndDate" -Value $collectionObject.ProjectEstimatedEndDate -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ProjectID" -Name "ProjectID" -Value $collectionObject.ProjectID -whatif -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ProjectManagerEmail" -Name "ProjectManagerEmail" -Value $collectionObject.ProjectManager -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ProjectName" -Name "ProjectName" -Value $collectionObject.ProjectName -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\ProjectPPDSponsor" -Name "ProjectPPDSponsor" -Value $collectionObject.ProjectPPDSponsor -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\PurposeOrApplication" -Name "PurposeOrApplication" -Value $collectionObject.PurposeOrApplication -WhatIf -Verbose}
      Invoke-Command -ComputerName $server -ScriptBlock{Set-ItemProperty -Path "HKLM:\SYSTEM\path\SystemDescription" -Name "SystemDescription" -Value $collectionObject.SystemDescription -WhatIf -Verbose }
      }
    • #210246
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I’m pretty unsure if you have a major missconception in your mind or if I do not understand what you’re trying to do. 😉

      If I understood correctly what you want your CSV file should look a kind of like this:

      ComputerName,ATOStatus,Network,PatchPhase,ProjectEstimatedEndDate,ProjectID,ProjectManagerEmail,ProjectName,ProjectPPDSponsor,PurposeOrApplication,SystemDescription
      tom,Active,present,full,indefinite,007,Manager@email.com,SuperProject,Powershell.org,Powershell Core,TestServer
      amber,Inactive,absent,basic,30 min,008,Manager@email.com,TheOtherProject,Microsoft,Windows Powershell,ProdServer

      It should have the target computername in it and on the same row in the other columns the other settings you want to set for this particular computer.

      Now you can import this structured data and use it for your loop and the contained Invoke-Command. You can put more than one command into the scriptblock:

      $settingsArray = Import-Csv -Path .\Desktop\RegistryFields.csv
      
      foreach ($element in $settingsArray) {
          Invoke-Command -ComputerName $element.ComputerName -ScriptBlock {
              param (
                  $ATOStatus,
                  $Network,
                  $PatchPhase,
                  $ProjectEstimatedEndDate,
                  $ProjectID,
                  $ProjectManagerEmail,
                  $ProjectName,
                  $ProjectPPDSponsor,
                  $PurposeOrApplication,
                  $SystemDescription
              )
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ATOStatus' -Name 'ATOStatus' -Value $ATOStatus -WhatIf -verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\Network' -Name 'Network' -Value $Network -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\PatchPhase' -Name 'PatchPhase' -Value $PatchPhase -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectEstimatedEndDate' -Name 'ProjectEstimatedEndDate' -Value $ProjectEstimatedEndDate -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectID' -Name 'ProjectID' -Value $ProjectID -whatif -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectManagerEmail' -Name 'ProjectManagerEmail' -Value $ProjectManager -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectName' -Name 'ProjectName' -Value $ProjectName -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectPPDSponsor' -Name 'ProjectPPDSponsor' -Value $ProjectPPDSponsor -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\PurposeOrApplication' -Name 'PurposeOrApplication' -Value $PurposeOrApplication -WhatIf -Verbose
              Set-ItemProperty -Path 'HKLM:\SYSTEM\path\SystemDescription' -Name 'SystemDescription' -Value $SystemDescription -WhatIf -Verbose 
          } -ArgumentList $element.ATOStatus,
              $element.Network,
              $element.PatchPhase,
              $element.ProjectEstimatedEndDate,
              $element.ProjectID,
              $element.ProjectManagerEmail,
              $element.ProjectName,
              $element.ProjectPPDSponsor,
              $element.PurposeOrApplication,
              $element.SystemDescription
      }

      Because the scriptblock of the Invoke-Command does not “see” the variables from outside the scriptblock you have provide it in a special way. This one here is one of them.
      I’d recommend making a step back and start with learning the very basics of Powershell from scratch. At least you should always read the help for the cmdlets you’re about to use. Please read it completely including the examples to learn how to use them. Another great source of knowledge is the The Unofficial PowerShell Best Practices and Style Guide.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.