Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

invoke-command and get-itemproperty

Posted on 2013-01-23
10
Medium Priority
?
1,510 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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

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

Are your AD admin tools letting you down?

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.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

664 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