【问题标题】:Cannot convert value "" to type "System.Char"无法将值“”转换为类型“System.Char”
【发布时间】:2016-12-07 16:20:05
【问题描述】:

您好,我尝试创建一个 PS 脚本来分配驱动器号、格式化并使用所需的分配单元大小标记驱动器。

CSV 文件(Storage_Data_Input.txt) 是这样的:

DiskNumber,DriveLetter,NewFileSystemLabel,AllocationUnitSize
7,N,Drive_N,4096
7,N,Drive_N,4096

脚本如下:

########################
# New Partition/Drive letter/Format/Label/Allocation
Measure-Command{ 

Clear


$Datalist = Import-Csv "H:\My Documents\My Powershell\Storage_Data_Input.txt"
$ServerList = Get-Content "H:\My Documents\My Powershell\serverlist.txt"

ForEach ($Item in $Datalist){
$DiskNumber=$($Item.DiskNumber)
$driveletter=$($Item.DriveLetter)
$NewFileSystemLabel=$($Item.NewFileSystemLabel)
$AllocationUnitSize=$($Item.AllocationUnitSize)


$c=Get-Credential Domain\Userid 

foreach ($ServerName in $ServerList){
Write-Host "Setting Drive_"$DriveLetter" on server "$ServerName""
Invoke-Command -ComputerName $ServerName -Credential $c -ScriptBlock {

Stop-Service -Name ShellHWDetection

New-Partition -DiskNumber "$DiskNumber" -UseMaximumSize -DriveLetter "$driveletter" | Format-Volume -FileSystem NTFS -NewFileSystemLabel "$NewFileSystemLabel" -AllocationUnitSize "$AllocationUnitSize" -Force -Confirm:$false

Start-Service -Name ShellHWDetection 

}}}}

执行时出现如下错误:

Cannot process argument transformation on parameter 'DriveLetter'. Cannot convert value "" to type "System.Char". Error: "String must be exactly one character long."
+ CategoryInfo          : InvalidData: (:) [New-Partition], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,New-Partition
+ PSComputerName        : Servername

【问题讨论】:

  • 我没有对此进行测试,但我认为在您的 CSV 中,驱动器号列应该只包含驱动器号“N”而不是“Drive_N”

标签: csv powershell csv-import


【解决方案1】:

如果您正在远程执行变量,则必须作为参数列表传递或使用“使用”提供程序引用。

例如

Invoke-Command -ComputerName $ServerName -Credential $c -ScriptBlock {
    Stop-Service -Name ShellHWDetection
    New-Partition -DiskNumber "$using:DiskNumber" -UseMaximumSize -DriveLetter "$using:driveletter" | Format-Volume -FileSystem NTFS -NewFileSystemLabel "$using:NewFileSystemLabel" -AllocationUnitSize "$using:AllocationUnitSize" -Force -Confirm:$false
    Start-Service -Name ShellHWDetection 
}

【讨论】:

  • 嗨,克里斯,非常感谢。我能够毫无错误地运行脚本。
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多