工具:PowerShell ISE
小弟最近再寫一支簡易的Script,該Script主要目的不是這次發問的討論重點,目前該Script可用性已滿足需求了,但是看到If底下那麼多重複的Code,整個看了就很「阿雜」
一直Google也沒找到什麼眉角,甚至連關鍵字都相當不確定,因此秉持著學習的精神,上來和大家請益,『我該如何把If...elseIf重複的Code變成一個Function來使用呢?』
廢話不再多說,直接上Code
for ($i= 0; $i -le 1; $i++){
$SIDNu = (import-Csv "D:\Script test\SID.csv").SID[$i]
$NameNu = (import-Csv "D:\Script test\SID.csv").Name[$i]
Mount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
$DiskNu = (Get-WmiObject -Class Win32_LogicalDisk -Filter "VolumeName='User Disk'" | Select-Object DeviceID).DeviceID
if ( $NameNu[-1] -eq "0" -or $NameNu[-1] -eq "1" )
{
New-Item "D:\Script test\PC1\$NameNu.bk\" -ItemType "directory"
$Source = "$DiskNu\Downloads"
$Destn = "D:\Script test\PC1\$NameNu.bk\Downloads"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Favorites"
$Destn = "D:\Script test\PC1\$NameNu.bk\Favorites"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Desktop"
$Destn = "D:\Script test\PC1\$NameNu.bk\Desktop"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
}
elseif( $NameNu[-1] -eq "2" -or $NameNu[-1] -eq "3")
{
$Source = "$DiskNu\Downloads"
$Destn = "D:\Script test\PC2\$NameNu.bk\Downloads"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Favorites"
$Destn = "D:\Script test\PC2\$NameNu.bk\Favorites"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Desktop"
$Destn = "D:\Script test\PC2\$NameNu.bk\Desktop"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
}
elseif( $NameNu[-1] -eq "4" -or $NameNu[-1] -eq "5")
{
$Source = "$DiskNu\Downloads"
$Destn = "D:\Script test\PC3\$NameNu.bk\Downloads"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Favorites"
$Destn = "D:\Script test\PC3\$NameNu.bk\Favorites"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Desktop"
$Destn = "D:\Script test\PC3\$NameNu.bk\Desktop"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
}
elseif( $NameNu[-1] -eq "6" -or $NameNu[-1] -eq "7")
{
$Source = "$DiskNu\Downloads"
$Destn = "D:\Script test\PC4\$NameNu.bk\Downloads"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Favorites"
$Destn = "D:\Script test\PC4\$NameNu.bk\Favorites"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Desktop"
$Destn = "D:\Script test\PC4\$NameNu.bk\Desktop"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
}
elseif( $NameNu[-1] -eq "8" -or $NameNu[-1] -eq "9")
{
$Source = "$DiskNu\Downloads"
$Destn = "D:\Script test\PC5\$NameNu.bk\Downloads"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Favorites"
$Destn = "D:\Script test\PC5\$NameNu.bk\Favorites"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\Desktop"
$Destn = "D:\Script test\PC5\$NameNu.bk\Desktop"
robocopy "$Source" "$Destn" /e /dcopy:t /xo /xj /MT:100
}
Dismount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
}
我也嘗試過使用「Powershell -c "Path"」的方式了,但因為中間有變數的情況下,又不曉得剛如何將變數值帶過去
函數化是對的
我是用字串連接變數的方式
因為沒寫過SHELL 幫你查了大概的用法
https://docs.microsoft.com/zh-tw/powershell/scripting/learn/deep-dives/everything-about-string-substitutions?view=powershell-7.2
希望有幫到你
另外可以考慮用switch
function Get-Path {
$DownPath = "Downloads"
$FavorPath = "Favorites"
$DeskPath = "Desktop"
$Source = "$DiskNu\$DownPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$DownPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\$FavorPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$FavorPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\$DeskPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$DeskPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
}
for ($i= 0; $i -le 1; $i++){
$SIDNu = (import-Csv "D:\Script test\SID.csv").SID[$i]
$NameNu = (import-Csv "D:\Script test\SID.csv").Name[$i]
Mount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
$DiskNu = (Get-WmiObject -Class Win32_LogicalDisk -Filter "VolumeName='User Disk'" | Select-Object DeviceID).DeviceID
switch ( $NameNu[-1] )
{
0 {$PCNu = "PC1";Get-Path}
1 {$PCNu = "PC1";Get-Path}
2 {$PCNu = "PC2";Get-Path}
3 {$PCNu = "PC2";Get-Path}
4 {$PCNu = "PC3";Get-Path}
5 {$PCNu = "PC3";Get-Path}
6 {$PCNu = "PC4";Get-Path}
7 {$PCNu = "PC4";Get-Path}
8 {$PCNu = "PC5";Get-Path}
9 {$PCNu = "PC5";Get-Path}
}
Dismount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
}
OK 搞定,感覺這樣濃縮已經很足夠了
看起來舒服多了!
恭喜
建議用參數的方式:
function Get-Path ( [String]$PCNu ) {
$DownPath = "Downloads"
$FavorPath = "Favorites"
$DeskPath = "Desktop"
$Source = "$DiskNu\$DownPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$DownPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\$FavorPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$FavorPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
$Source = "$DiskNu\$DeskPath"
$Destn = "D:\Script test\$PCNu\$NameNu.bk\$DeskPath"
robocopy $Source $Destn /e /dcopy:t /xo /xj /MT:100
}
for ($i= 0; $i -le 1; $i++){
$SIDNu = (import-Csv "D:\Script test\SID.csv").SID[$i]
$NameNu = (import-Csv "D:\Script test\SID.csv").Name[$i]
Mount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
$DiskNu = (Get-WmiObject -Class Win32_LogicalDisk -Filter "VolumeName='User Disk'" | Select-Object DeviceID).DeviceID
#看起來可以用計算的: 0,1-->1; 2,3-->2,...
Get-Path "PC"+[Math]::Truncate(($NameNu[-1]+2)/2)
Dismount-DiskImage -ImagePath "D:\Script test\Disk\UVHD-$SIDNu.vhdx"
}