Solved

invoke-command and get-itemproperty

Posted on 2013-01-23
10
1,337 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
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
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
Comment Utility
What is the value of $inst_name?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
The value of $inst_name is MSSQL$MX_78_RT Thanks bibi
0
 

Author Comment

by:bibi92
Comment Utility
$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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

13 Experts available now in Live!

Get 1:1 Help Now