Welcome › Forums › General PowerShell Q&A › registry changes
- This topic has 10 replies, 3 voices, and was last updated 10 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
March 12, 2020 at 10:25 pm #210000
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 hereInvoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {Set-ItemProperty -Path “HKLM:\SYSTEM\path\Status” -Name “status” -Value $collectionObject.status}
-
March 13, 2020 at 12:53 am #210015
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:
PowerShell123456789101112$inputData = @'Name of Computer,Status,Hardware,Scope,DurationTom,Active,present,full,indefiniteAmber,delayed,installed,full,30 days'@ |ConvertFrom-Csv$credential = Get-Credential -Credential #add creds hereForeach ($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 }}-
March 13, 2020 at 1:45 pm #210108
Thank you for the assistance but I have two items that are not presenting correctly:
- 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
-
-
March 13, 2020 at 2:19 am #210018PowerShell123456789$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 entryInvoke-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
-
March 13, 2020 at 2:03 pm #210123
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?
-
-
March 13, 2020 at 2:09 pm #210129
… 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.
-
March 13, 2020 at 9:05 pm #210225
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.-
This reply was modified 10 months, 2 weeks ago by
manofstyle0456.
-
This reply was modified 10 months, 2 weeks ago by
-
March 13, 2020 at 9:17 pm #210231
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.
PowerShell1234567891011121314151617181920212223242526272829303132333435363738$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].SystemDescriptionforeach($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 }} -
March 14, 2020 at 12:34 am #210246
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:
PowerShell123ComputerName,ATOStatus,Network,PatchPhase,ProjectEstimatedEndDate,ProjectID,ProjectManagerEmail,ProjectName,ProjectPPDSponsor,PurposeOrApplication,SystemDescriptiontom,Active,present,full,indefinite,007,Manager@email.com,SuperProject,Powershell.org,Powershell Core,TestServeramber,Inactive,absent,basic,30 min,008,Manager@email.com,TheOtherProject,Microsoft,Windows Powershell,ProdServerIt 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:
PowerShell12345678910111213141516171819202122232425262728293031323334353637$settingsArray = Import-Csv -Path .\Desktop\RegistryFields.csvforeach ($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 -verboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\Network' -Name 'Network' -Value $Network -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\PatchPhase' -Name 'PatchPhase' -Value $PatchPhase -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectEstimatedEndDate' -Name 'ProjectEstimatedEndDate' -Value $ProjectEstimatedEndDate -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectID' -Name 'ProjectID' -Value $ProjectID -whatif -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectManagerEmail' -Name 'ProjectManagerEmail' -Value $ProjectManager -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectName' -Name 'ProjectName' -Value $ProjectName -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\ProjectPPDSponsor' -Name 'ProjectPPDSponsor' -Value $ProjectPPDSponsor -WhatIf -VerboseSet-ItemProperty -Path 'HKLM:\SYSTEM\path\PurposeOrApplication' -Name 'PurposeOrApplication' -Value $PurposeOrApplication -WhatIf -VerboseSet-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.
-
-
AuthorPosts
- The topic ‘registry changes’ is closed to new replies.