Gather Remote Active NIC info with Domain Creds

I am gathering info from separate AD Domains and updating a DB on the front-end side and I have having issues with adding a single quote for the active NIC. The -filter and the -InterfaceAlias needs a single quote and I found a way to add the single quote with a varible “‘{0}"‘“ -f $var, but it will not pass as an Invoke-Command with Creds. If you have any ideas, please let me know. Here is the code:

param([string] $ServerName,[string] $NICInfoType,[int] $NICIndex,[string] $NICConnID,[string] $varKEYUser,[string] $varPassKey)

$global:adminCreds = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $varKEYUser, (ConvertTo-SecureString -String $varPassKey -AsPlainText -Force))
$global:ComputerName = $ServerName
$global:NIndex = "InterfaceIndex = " + $NICIndex.ToString()
$global:QUOTECHRNIndex = “‘{0}’” -f $global:NIndex
$global:NetConnectionID = $NICConnID.ToString()
$global:QUOTENetConnectionID = “‘{0}’” -f $global:NetConnectionID

switch ($NICInfoType) {
“NICInfo” {
Invoke-Command -ComputerName $global:ComputerName -ScriptBlock { Get-WmiObject win32_networkadapter -filter ‘netconnectionstatus = 2’ | select netconnectionid, name, InterfaceIndex, netconnectionstatus } -Credential $global:adminCreds
}
“GWDHCPSMInfo” {
$Session = New-PSSession -ComputerName $global:ComputerName -Credential $global:adminCreds
$scriptBlock = { Get-WmiObject Win32_NetworkAdapterConfiguration -filter $global:QUOTECHRNIndex | select DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway }
Invoke-Command -Session $Session -ScriptBlock $scriptBlock
}
“DNSInfo” {
Write-Host $($global:QUOTENetConnectionID)
Invoke-Command -ComputerName $global:ComputerName -ScriptBlock { Get-DnsClientServerAddress -InterfaceAlias $($global:QUOTENetConnectionID) | select ServerAddresses, AddressFamily } -Credential $global:adminCreds
}
}l:adminCreds
}
}

I haven’t tried this, but shouldn’t your code be:


$global:QUOTECHRNIndex = “InterfaceIndex = ‘{0}’” -f $NICIndex.ToString()


$scriptBlock = {
Get-WmiObject Win32_NetworkAdapterConfiguration -filter $global:QUOTECHRNIndex |
Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway
}

Thank you for simplifying. So after testing with these updates you made the $QUOTECHRNIndex only shows as a string not the values (added a Write-Host $scriptBlock to show this):

Get-WmiObject Win32_NetworkAdapterConfiguration -filter $global:QUOTECHRNIndex | Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway

I can build it with this

$scriptBlock = “{ Get-WmiObject Win32_NetworkAdapterConfiguration -filter " + $global:QUOTECHRNIndex + " | Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway }”

with a result of this:

{ Get-WmiObject Win32_NetworkAdapterConfiguration -filter InterfaceIndex = ‘4’ | Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway }
Invoke-Command : Cannot bind parameter ‘ScriptBlock’. Cannot convert the “{ Get-WmiObject
Win32_NetworkAdapterConfiguration -filter InterfaceIndex = ‘4’ | Select-Object DHCPEnabled, DHCPServer, DNSDomain,
DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway }” value of type “System.String” to type
“System.Management.Automation.ScriptBlock”.
At C:\Apps\Comptrack\NICInfoDiffCreds.ps1:17 char:49

  •     Invoke-Command -Session $Session -ScriptBlock $scriptBlock
    
  •                                                   ~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Invoke-Command], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommandndCommand

However on the previous code the variable show the single quotes in the right place with the built ScriptBlock and converting to Ssystem.Management.Automating.ScriptBlock:

	$scriptBlockSysStr = [Scriptblock]::Create($scriptBlock)
	Write-Verbose -Message $scriptBlockSysStr.GetType().FullName -Verbose
	Invoke-Command -Session $Session -ScriptBlock $scriptBlockSysStrtr

The results from Invoke-Command are not run but a string output of the converted variable are output:

VERBOSE: System.Management.Automation.ScriptBlock
Get-WmiObject Win32_NetworkAdapterConfiguration -filter ‘InterfaceIndex = 4’ | Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGatewayy

The scriptblock is in correct format now to run, but is seems like I am missing somthing.

I got it to work now. Here is the code under the GWDHCPSMInfo (remove brackets { } before converting):

"GWDHCPSMInfo" {
	$Session = New-PSSession -ComputerName $global:ComputerName -Credential $global:adminCreds 
	$scriptBlock = "Get-WmiObject Win32_NetworkAdapterConfiguration -filter " + $global:QUOTECHRNIndex + " | Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway"
	$scriptBlockSysStr = [Scriptblock]::Create($scriptBlock)
	Invoke-Command -Session $Session -ScriptBlock $scriptBlockSysStrsStr

I think your use of scope-qualified variables. I.e., all those “$global:” scopes just clutters up the code. Is there a reason you’re using explicit scoping?

Using a scriptblock to convey information to a remote machine means that the value of variables from the local machine have to be passed to the remote machine IN the scriptblock. The easiest way to do this is to prefix the local variable name with the “using:” prefix within the scriptblock. Or you can use a “param()” inside the scriptblock and pass the variable name in the -ArgumentList parameter of the Invoke-Code cmdlet.

In my original reply I forgot to include the “using:”. Here’s the corrected line:

Get-WmiObject Win32_NetworkAdapterConfiguration -filter $using:global:QUOTECHRNIndex

Here’s a way to do this with the -ArgumentList parameter:

$filter = "InterfaceIndex = '{0}'" -f $NICIndex # no need for the .ToString

$scriptBlock = {
                param($filter)
                Get-WmiObject Win32_NetworkAdapterConfiguration -filter $filter | 
                    Select-Object DHCPEnabled, DHCPServer, DNSDomain, DNSDomainSuffixSearchOrder, IPsubnet, DefaultIPGateway 
               }
Invoke-Command -Session $Session -ScriptBlock $scriptBlock -ArgumentList $filter

EDIT: Corrected the placement of the “$” before the “using:”

1 Like

Shouldn’t that actually be:

Get-WmiObject Win32_NetworkAdapterConfiguration -filter $using:QUOTECHRNInd

?

You’re correct about the placement of the “$”. It should go in front of the entire variable reference (which includes the scope). That’s what comes from not actually testing the code I write. My bad.

But in your correction you used “$using:QUOTECHRNInd” without the scope keyword. That’ll work in the context of the given code example, but if that same “QUOTECHRNInd” had also been created using another scope (i.e., local or script) then the variable with the most limited scope would have been used.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.