Script for new registry in remote computers

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 2 years, 4 months ago.

  • Author
    Posts
  • #17594
    Profile photo of Prashant Bande
    Prashant Bande
    Participant

    Hi all,
    I am trying to create a script with a function where I need to create new registry on multiple remote computers. I am getting below error on passing parameters to the script:
    Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
    At D:\6420×64\New-SupportLevel4.ps1:44 char:13
    + Invoke-Command -ComputerName $HostName
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

    Attached is my ps1 file.

  • #17595
    Profile photo of Prashant Bande
    Prashant Bande
    Participant

    M request to upload ps1 was denied:) . Pasting contents of my ps1 file:

    function Set-RegEntry
    {
    [CmdletBinding()]
    #[OutputType([int])]
    Param
    (

    #This is ComputerName Parameter for target Computer.
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true,
    Position=0)]
    [String[]]$HostName,

    # This is New Registry Parameter for target Computer.
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$false,
    ValueFromPipelineByPropertyName=$false,
    Position=1)]
    [ValidateSet(1,2,3)] [int]$RegLevel
    )

    Begin
    {
    #Setting up registry path to check
    $RegistryKey = 'HKLM:\SOFTWARE\CMC'
    $PathTest = Test-Path $RegistryKey
    }
    Process
    {

    Try{
    #Invoking command on list of remote computers stored in $HostName
    Invoke-Command -ComputerName $HostName
    {

    #Checking if registry path exists
    IF ($PathTest -eq $false)
    {
    #If not, creating new registry path and setting up the reistry value
    NEW-ITEM -Path HKLM:\SOFTWARE -Name CMC
    NEW-ITEM -Path HKLM:\SOFTWARE\CMC\ -Name "Support Level" -Value $RegLevel
    }
    ELSE
    {
    #If registry exists
    WRITE-HOST "Registry Key Exists on $HostName"
    }
    }
    Catch [PSRemotingTransportException]
    {
    #Catching exception if there is an issue on powershell on remote computer.
    Write-Output "Issue with Powershell on $HostName"
    }
    }
    Finally{}
    }
    End
    {
    }
    }

  • #17599
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Try this:

    function Set-Reg {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,HelpMessage='Target host name or IP address')][String[]]$HostName,
            [Parameter(Mandatory=$true,Position=1,HelpMessage='Registry container/folder')][String]$RegKey,
            [Parameter(Mandatory=$true,Position=2,HelpMessage='Registry leaf')]$RegValue,
            [Parameter(Mandatory=$true,Position=3,HelpMessage='Data to be saved/updated')]$RegData
        )
        $RemoteCommand = Invoke-Command -ComputerName $HostName { param($RegKey,$RegValue,$RegData)
            if (-not (Test-Path -Path $RegKey)) { New-Item -Path $RegKey }
            Set-ItemProperty -Path $RegKey -Name $RegValue -Value $RegData
            Get-ItemProperty -Path $RegKey -Name $RegValue
            } -ArgumentList $RegKey,$RegValue,$RegData
        if ($RemoteCommand.$RegValue -eq $RegData) { Write-Output "Registry key $Regkey\$RegValue set to $RegData" 
            } else { Write-Output "Failed to set registry key $Regkey\$RegValue" }
    }
    

    Example usage:

    Set-Reg localhost 'HKLM:\SOFTWARE\CMC' 'Support Level' 2
    

    when using this on remote computers, you must be running the script in a security context that has administrative permissions on the remote machines, otherwise the function can be modified to use -credential parameter..

    Note that PS will automatically pick the RegValue data type that matches the RegData,
    for example this command

     Set-Reg localhost 'HKLM:\SOFTWARE\CMC' 'Support Level' 2 

    creates a DWORD (32-bit) RegValue (leaf object) called 'Support Level' and stores RegData (2) in it.
    if followed by this command:

     Set-Reg localhost 'HKLM:\SOFTWARE\CMC' 'Support Level' 'cheese' 

    PS deletes the DWORD (32-bit) RegValue 'Support Level' and creates a new String RegValue 'Support Level' and stores RegData ('cheese') in it.

You must be logged in to reply to this topic.