PowerShell函数抛出空异常 [英] Powershell function throwing null exception

查看:448
本文介绍了PowerShell函数抛出空异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经阅读了关于调用函数并在PowerShell中传递参数的问题,但也许我不理解某些东西。我正在调用一个函数并传递几个参数,一切正常。它是在函数中定义的变量,当它不是时,它似乎为空。



函数:

 function get-session {
$ session = New-PSSession -ComputerName $ ip -Credential $ cred -Auth CredSSP
$ subcomps ='COMP1'
foreach ($子计算机中的$ Computer)
{
$计算机
Invoke-Command -Session $ session -ScriptBlock {Get-WmiObject -ComputerName $计算机 - 查询SELECT * FROM Win32_Group}

Remove-PSSession -ComputerName $ ip

$ / code>

脚本:

  $ ip ='123.45.67.89'
$ ComputerName =HOPCOMP
$ user =' \Admin'
$ cred = $ ComputerName + $ user
$ ip
$ cred
(get-session -ComputerName $ ip -Credential $ cred)

当我执行此操作时:

 无法验证参数'ComputerName'的参数。参数为空或空。 
提供一个非空或空的参数,然后再次尝试该命令。
+ CategoryInfo:InvalidData:(:) [Get-WmiObject],ParameterBindingValidationException
+ FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

当然,如果我将函数Get-WMIObject行中的$ Computer更改为COMP1,则一切正常。但是,在$ foreach循环中写出的$ Computer会成功写入COMP1。



我在这里缺少什么?

解决方案

您需要使用Invoke-Command指定-ArgumentList参数。例如:
$ b $ pre $ Invoke-Command -Session $ session -ScriptBlock {param($ Computer)Get-WmiObject -ComputerName $ Computer - QuerySELECT * FROM Win32_Group} -ArgumentList $ Computer


Okay, so I've read up on calling functions and passing parameters in powershell but maybe I'm not understanding something. I am calling a function and passing a couple parameters and everything works fine. It is variable defined within the function that seems to be null when it isn't.

Function:

function get-session {
$session = New-PSSession -ComputerName $ip -Credential $cred -Auth CredSSP
$subcomps = 'COMP1'
foreach ($Computer in $subcomps)
{
$Computer
Invoke-Command -Session $session -ScriptBlock { Get-WmiObject -ComputerName $Computer -Query "SELECT * FROM Win32_Group" }
}
Remove-PSSession -ComputerName $ip
}

Script:

$ip = '123.45.67.89'
$ComputerName = "HOPCOMP"
$user = '\Admin'
$cred = $ComputerName + $user
$ip
$cred
(get-session -ComputerName $ip -Credential $cred)

When I run this:

Cannot validate argument on parameter 'ComputerName'. The argument is null or empty.
Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Of course, if I change $Computer in in the function Get-WMIObject line to COMP1 everything works great. But, the $Computer write out in the foreach loop writes COMP1 successfully.

What am I missing here?

解决方案

You need to specify the -ArgumentList parameter with your Invoke-Command. For example:

Invoke-Command -Session $session -ScriptBlock { param($Computer) Get-WmiObject -ComputerName $Computer -Query "SELECT * FROM Win32_Group" } -ArgumentList $Computer

这篇关于PowerShell函数抛出空异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆