Solved

invoke-command and get-itemproperty

Posted on 2013-01-23
10
1,401 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
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now