Solved

invoke-command and get-itemproperty

Posted on 2013-01-23
10
1,348 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
 

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
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

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 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

11 Experts available now in Live!

Get 1:1 Help Now