为什么我的变量在Powershell中中断?(从文件中抓取变量)



STACKOVERFLOW中的第一篇文章!直到现在,我才问过任何问题。拜托,不要打我太重,哈哈。

我正在尝试创建一个 powershell 脚本来获取在配置文件中输入的名称并将其输入到脚本中。(重命名计算机(

它以前工作过,我发誓...就像原样。但是我最近将所有文件都移到了另一个测试系统,现在它显示这个......就像它无法阅读或看到任何东西一样。

我尝试测试它,看看它是否从正确的位置读取/收集,所以我放了一个写主机看看它是什么,它确实读取正确。

我把 -TotalCount 放进去抓住第二行,这是正确的 - 对吧?

为什么会失败。更愚蠢的是,它只是在工作。

法典:

$password =XXXXXXXX | ConvertTo-SecureString -asPlainText -Force 
$username = XXXXXXXX 
$credential = New-Object System.Management.Automation.PSCredential($username,$password) 
$compname = (Get-Content -path .CONFIGURATION.txt -TotalCount 2) 
Rename-Computer -NewName $compname -LocalCredential $credential -PassThru 
Write-Host $compname

错误:

Rename-Computer : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 
'NewName'. Specified method is not supported.
At line:6 char:26
+ Rename-Computer -NewName $compname -LocalCredential $credential -Pass ...
+                          ~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Rename-Computer], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameComputerCommand 

我的失败的屏幕截图

感谢 SID 的回答(下面的代码有效(谢谢!!!!!!可以说出我耳朵后面有多湿。

$password =XXXXXXXX | ConvertTo-SecureString -asPlainText -Force 
$username = XXXXXXXX 
$credential = New-Object System.Management.Automation.PSCredential($username,$password) 
$compname = (Get-Content -path .CONFIGURATION.txt)[1]
Rename-Computer -NewName $compname -LocalCredential $credential -PassThru 
Write-Host $compname

我把-TotalCount 2放进去抓住第二行,这是正确的 - 对吧?

否:-TotalCount(其别名为-First-Head(指定要从文件(开头(读取的行数。

如果您的文件至少有 2 行,

$compname = (Get-Content -path .CONFIGURATION.txt -TotalCount 2)

因此,将在$compname中存储一个2 元素数组([object[]](,并且鉴于Rename-Computer(明智地(仅通过其-NewName参数接受单个计算机名称,您将收到您看到的错误消息。


从文件中获取一行:

对于文件,最简单但有些浪费的方法是Get-Content将所有输入行读取到数组中,然后简单地索引到该数组中(Sid 建议的方法(:

# Line with (0-based) index *1* is the *2nd line.
# Note: Convenient, but reads the entire file first.
$compname = (Get-Content -path .CONFIGURATION.txt)[1]

一种更有效但仍然可能浪费的方法,这也是本例中最快的变体(直接调用 .NET 方法(:

# Collect only the first 2 lines in an array, then
# use [-1] to grab the *last* line from that array, which is by 
# definition the 2nd one.
$compname = (Get-Content -path .CONFIGURATION.txt -First 2)[-1]

节省内存但最慢的方法是通过管道传输到Select-Object并使用其(基于0(
-Index参数:

# Selects (outputs) only the line with index 1, i.e. the 2nd line.
# This is most memory-efficient, because by using the pipeline 
# the lines preceding the line of interest merely *stream* 
# (are processed one by one), without the need to first *collect them in an array*.
$compname = Get-Content -path .CONFIGURATION.txt | Select-Object -Index 1

从文件中获取行 - 按范围和/或不同的索引:

请注意,-Index可以选择支持索引数组以支持返回,在此示例中使用..、范围(数字/字符(运算符:

$lines2and3 = Get-Content -path .CONFIGURATION.txt | Select-Object -Index (1..2)

PowerShell的索引运算符[...]提供了相同的灵活性

$lines2and3 = (Get-Content -path .CONFIGURATION.txt)[1..2]

您甚至可以组合多个范围和不同的索引,使用表达式指定索引:

# Get line 2, as well as lines 4 - 6.
# Note the need for unary `,` if the expression starts with a *single* index.
# You wouldn't need that if the expression starts with an array; e.g.:
#    `3..5 + 1` or `3, 4, 5 + 1`
Get-Content -path .CONFIGURATION.txt | Select-Object -Index (, 1 + 3..5)
# Ditto.
(Get-Content -path .CONFIGURATION.txt)[, 1 + 3..5]

最新更新