如果没有菜单,switch语句不会旋转

时间:2015-08-10 19:18:12

标签: powershell switch-statement

我正在运行下面的脚本,当我执行语句时,它会同时运行所有3个语句,因此它会返回到原始位置。

原始代码vv

fsutil reparsepoint query "d:/tf90/test" > outfile.txt



$path = Get-Content D:\tf90\outfile.txt | Select-Object -last 5 


$choice = @('1','2','3')

switch ($choice)
{
'1' {$path = ' 0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 64 00 3a 00  ..".........d.:.
     0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
     0020:  73 00 74 00 31 00 5c 00  3f 00 3f 00 5c 00 64 00  s.t.1.\.?.?.\.d.
     0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
     0040:  65 00 73 00 74 00 31 00                           e.s.t.1.'

 remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test2 }

'2' {$path='
0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 32 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.2.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 32 00                           e.s.t.2.'

 remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test3 }

'3' {$path='0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 33 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.3.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 33 00                           e.s.t.3. '
remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test1 }
}

我在声明中添加了一个菜单,它可以工作但不是我需要做的自动化。

新代码--- vvvv ----

fsutil reparsepoint query "d:/tf90/test" > outfile.txt

Get-Content D:\tf90\outfile.txt | Select-Object -last 5 | Write-Host -ForegroundColor green

$path = Get-Content D:\tf90\outfile.txt | Select-Object -last 5 


$menu = ('1','2','3')
$choice = $Menu | Out-GridView -OutputMode Multiple -Title 'Select Locations you want to run staging, and click OK.'

switch ($choice)
{
'1' {$path = ' 0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 64 00 3a 00  ..".........d.:.
     0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
     0020:  73 00 74 00 31 00 5c 00  3f 00 3f 00 5c 00 64 00  s.t.1.\.?.?.\.d.
     0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
     0040:  65 00 73 00 74 00 31 00                           e.s.t.1.'

 remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test2 }

'2' {$path='
0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 32 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.2.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 32 00                           e.s.t.2.'

 remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test3 }

'3' {if ($path='0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 33 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.3.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 33 00                           e.s.t.3. ')
{remove-item -force d:\tf90\test;

mklink test/d d:\tf90\test1 }
}
}

我试图让switch语句自动使用其中一个选项,然后在读取outfile时执行它。我需要将输出放入$path并让switch语句查看输出是什么,然后转到其中一个选项来执行匹配$path

的命令

1 个答案:

答案 0 :(得分:1)

PowerShell 5.0:

PowerShell 5.0在文件系统提供程序中添加了对reparsepoints的支持:

$Link = Get-Item D:\tf90\test
$TargetFileName = [System.IO.Path]::GetFileName($Link.Target)

switch($TargetFileName) {
    "test1" { # rotate to test2 }
    "test2" { # rotate to test3 }
    "test3" { # rotate to test1 }
}

PowerShell 4.0及更早版本:

如果要使用开关将fsutil的输出与已知字符串进行比较,则需要将该字符串用作开关内的选择项。

为避免在开关体内部为每种可能性设置五行字符串,请将它们存储在变量中:

# run reparsepoint query
fsutil reparsepoint query "d:/tf90/test" > outfile.txt

# grab the relevant portion of the file
Write-Host ($ReparsePointPath = Get-Content D:\tf90\outfile.txt | Select-Object -Last 5) -ForegroundColor Green

# define the strings you want to compare it to
$Path1 = @'
0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 64 00 3a 00  ..".........d.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 31 00 5c 00  3f 00 3f 00 5c 00 64 00  s.t.1.\.?.?.\.d.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 31 00                           e.s.t.1.
'@
$Path2 = @'
0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 32 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.2.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 32 00                           e.s.t.2.
'@
$Path3 = @'
0000:  1a 00 22 00 00 00 1a 00  00 00 00 00 44 00 3a 00  ..".........D.:.
0010:  5c 00 74 00 66 00 39 00  30 00 5c 00 74 00 65 00  \.t.f.9.0.\.t.e.
0020:  73 00 74 00 33 00 5c 00  3f 00 3f 00 5c 00 44 00  s.t.3.\.?.?.\.D.
0030:  3a 00 5c 00 74 00 66 00  39 00 30 00 5c 00 74 00  :.\.t.f.9.0.\.t.
0040:  65 00 73 00 74 00 33 00                           e.s.t.3.
'@

# Run the output through the switch
switch ($ReparsePointPath){
    $Path1 {
        Remove-Item -Force d:\tf90\test
        cmd /c mklink test/d d:\tf90\test2
    }

    $Path2 {
        Remove-Item -Force d:\tf90\test
        cmd /c mklink test/d d:\tf90\test3
    }

    $Path3 {
        Remove-Item -Force d:\tf90\test
        cmd /c mklink test/d d:\tf90\test1
    }
}
相关问题