[英]You cannot call a method on a null-valued expression
Not having much luck with my Powershell script, so any help would be great! 我的Powershell脚本没有太多运气,所以任何帮助都会很棒! What I'm trying to do is to reference from a test file that contains lines of usernames, and compare it to the Excel spreadsheet on column A1.
我要做的是从包含用户名行的测试文件中引用,并将其与A1列上的Excel电子表格进行比较。 If exists, it should blank out the username.
如果存在,则应该删除用户名。
$users = Get-Content "E:\temp\test.txt"
foreach ($user in $users) {
set-aduser $user -fax " "
$answer1 = read-host "Please Make a Selection"
if ($answer1 -eq 1){
$location="Melbourne"
}
if ($answer1 -eq 2){
$location="Sydney"
}
if ($answer1 -eq 3){
$location="Adelaide"
}
if ($answer1 -eq 4){
$location="Brisbane"
}
if ($answer1 -eq 5){
$location="Perth"
}
$ExcelPath = 'E:\temp\FX MFD UserPIN.xlsx'
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)
$ExcelWorkSheet = $Excel.WorkSheets.item("$location")
$Range = $ExcelWorkSheet.Range("A1").EntireColumn
$Search = $Range.find($user)
If($Search.value() -contains $user)
{
Write-Host "User FOUND, removing now"
$Search.value() = ""
}
else {
Write-Host "User NOT FOUND"
}
}
Error code is this: 错误代码是这样的:
You cannot call a method on a null-valued expression.
At E:\temp\testsest.ps1:35 char:12
+ If($Search.value() -contains $SearchString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
How do I replace the $Search to get $user ? 如何更换$ Search以获取$ user ?
Range.Find()
returns Nothing if no match is found. 如果未找到匹配项,
Range.Find()
将返回Nothing。
Therefore, before you check if there is a match, you have to check if there's a null value in $Search
. 因此,检查前如果有比赛,你必须检查是否有一个空值
$Search
。
if ($Search -ne $null) {
if ($Search.value -contains $user)
{
Write-Host "User FOUND, removing now"
$Search.value = ""
}
else {
Write-Host $Search.value," what did we find?!"
}
}
else {
Write-Host "User NOT FOUND"
}
Also, value
is not a method, it's a property, so don't use round brackets. 此外,
value
不是方法,它是属性,因此不要使用圆括号。
Thanks Vesper. 谢谢Vesper。 I've made a couple more changes and can confirm it works now.
我做了一些更改,可以确认它现在有效。
if ($Search -ne $null) {
if ($Search.text -contains $user)
{
Write-Host "User FOUND, removing now"
$Range.replace($Search,"")
}
else {
Write-Host $Search.text," what did we find?!"
}
}
else {
Write-Host "User NOT FOUND"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.