• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

无法将值“”转换为键入“System.Char”

csv 来源:Bala MS 6次浏览

您好我试图创建一个PS脚本来分配驱动器的字母,格式和标签与所需的分配单元大小的驱动器。无法将值“”转换为键入“System.Char”

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 


===========解决方案如下:

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

例如

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 
} 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)