Solved

invoke-command and get-itemproperty

Posted on 2013-01-23
10
1,442 Views
Last Modified: 2013-01-23
Hello,

foreach($inst in $list_inst) {
	$inst_name = $inst.name.replace("MSSQL$","")

	$comp_name1 = (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$inst_name
	$key_name = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\"+$comp_name1 + "\Cluster"
	$key_prop = get-itemproperty -path $key_name -ErrorAction silentlycontinue 
	if($key_prop -eq $null) {
		$ser_name = (Get-WmiObject Win32_Computersystem).Name
	}
	else {
		$ser_name = $key_prop.ClusterName
	}
}

Open in new window

     I have to modify these following commands for access remote registry because this error is returned :
      Get-ItemProperty : Cannot find path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Ser
      ver\Instance Names\SQL' because it does not exist.

      $comp_name1 = (Invoke-Command -ComputerName $mach_name -ScriptBlock { (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$inst_name})
      $key_name = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\"+$comp_name1 + "\Cluster"
      $key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itemproperty -path $key_name)-ErrorAction silentlycontinue }


      But $comp_name1 is null because the invoke-command is not correct and $key_prop failed with the following error :
      Cannot bind argument to parameter 'Path' because it is null.
          + CategoryInfo          : InvalidData: (:) [Get-ItemProperty], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemPropertyCommand



How can I resolve this problem ?

Thanks

Regards

Bibi
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38809292
Try to pass variable as a argument to ScriptBlock..
$comp_name1 = (Invoke-Command -ComputerName $mach_name -ScriptBlock { (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$args[0]}) -Args $inst_name

Open in new window

0
 

Author Comment

by:bibi92
ID: 38809309
Hello,

Thanks I have tested :
You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:175
+ $comp_name1 = (Invoke-Command -ComputerName $mach_name -ScriptBlock { (get-itemproperty -path "HKLM:\SOFTWARE\Microso
ft\Microsoft SQL Server\Instance Names\SQL").$args[0]}) - <<<< Args $inst_name
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

Regards
bibi
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38809324
$comp_name1 = (Invoke-Command -ComputerName $mach_name -ScriptBlock { (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$args[0]} -Args $inst_name) 

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:bibi92
ID: 38809332
Hello,

Thanks, but I have another error :
Cannot index into a null array.
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Regards

bibi
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38809363
What is the value of $inst_name?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38809365
Also Also did you modify the following line with argument?
$key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itemproperty -path $key_name)-ErrorAction silentlycontinue }
0
 

Author Comment

by:bibi92
ID: 38809512
The value of $inst_name is MSSQL$MX_78_RT Thanks bibi
0
 

Author Comment

by:bibi92
ID: 38809519
$key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itempropert
y -path $key_name) -ErrorAction silentlycontinue }
You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:115
+ $key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itemproperty -path $key_name) - <<
<< ErrorAction silentlycontinue }
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38809558
If you want to add -ErrorAction silentlycontinue for get-itemproperty, then it should be inside the bracket..
$key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itemproperty -path $key_name -ErrorAction silentlycontinue) }

Open in new window

Else if it is for Invoke-Command the it should be outside the script block..
$key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {$key_prop = (get-itemproperty -path $key_name) } -ErrorAction silentlycontinue

Open in new window


Also pass the $key_name as a argument to ScriptBlock..
$key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {get-itemproperty -path $args[0] } -Args $key_name -ErrorAction silentlycontinue

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 38810347
The final result is:
      $comp_name1 = (Invoke-Command -ComputerName $mach_name -ScriptBlock { (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$args} -Args $inst_name)
      $key_name = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\"+$comp_name1 + "\Cluster"
      $key_prop = Invoke-Command -ComputerName $mach_name -ScriptBlock {get-itemproperty -path $args[0] } -Args $key_name -ErrorAction silentlycontinue
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question