2016-08-02 108 views
0

您好我試圖創建一個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 
+0

我還沒有測試這一點,但我想在您的CSV的驅動器號列應該只包含驅動器字母「N」,而不是「Drive_N」 – Kiran

回答

3

如果您'執行遠程變量必須通過作爲參數列表傳遞或使用「使用」提供程序引用。

例如

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

嗨克里斯,謝謝你這麼非常。我能夠無誤地運行腳本。 –

相關問題