繁体   English   中英

脚本在 Powershell CLI 中有效,但在 ISE 中或作为 PS1 无效

[英]Script Works in Powershell CLI but not in ISE or as a PS1

当我在 ISE 中运行脚本时(以管理员身份)出现错误:

Get-ADComputer : Cannot find an object with identity: 'W02439'
+ $WGUID = (Get-ADComputer -Identity $W).ObjectGUID
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (W02439:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
 

在 Powershell 中运行,我得到预期的 output,我仔细检查过的 GUID 编号是正确的 ID。

$WNumbers = 
'
W02418,
W02427,
W02430,
W02434,
W02438,
W02439
'

$WNumbers = $WNumbers.Replace("`n",'')
$WNumbers = $WNumbers.Trim()
$WNumbers = $WNumbers.Split(",")

$WNumbers

foreach($W in $WNumbers) {
$WGUID = (Get-ADComputer -Identity $W).ObjectGUID
Write-Host $WGUID
#Add-ADGroupMember -Identity $AOGUID -Members $WGUID -WhatIf
}

我想也许 ISE 没有 ActiveDirectory,所以我最初放置了 Import-Module ActiveDirectory - 这并没有解决它。 我还尝试重新启动 ISE,以非管理员身份运行它,但均无效。 但是当我运行时:

Get-ADComputer -Identity "W02418" 

它按预期工作。 所以我认为我的问题是它不允许我将变量传递到 ISE 引擎与 PS 内部的 -Identity 中。 (通常数字会存储在文件中并读入等,但我想将代码简化到问题似乎所在的位置,并且将变量传递给 -Identity)

$wnumbers 中也可能有“`r”。 它可能不会显示在控制台中。 我知道脚本与控制台的行尾不同,但 ise 与控制台不同。

$WNumbers = 
'
W02418,
W02427,
W02430,
W02434,
W02438,
W02439
'
$WNumbers = $WNumbers.Replace("`n",'')
$WNumbers -replace '\r','\r'

\rW02418,\rW02427,\rW02430,\rW02434,\rW02438,\rW02439\r

我会做:

$WNumbers = -split 'W02418
W02427
W02430
W02434
W02438
W02439'

要么

$WNumbers = echo W02418 W02427 W02430 W02434 W02438 W02439

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM