2011-12-14 75 views
0

我想循环使用powershell创建本地共享并在5分钟后将其删除。然后等待1分钟和5分钟后重新创建共享。然后将其删除,并接连一分钟的时间,再等等等等创建它..Powershell循环

得到了那些二:

$FolderPath = "D:\proxy" 
$ShareName = "proxy" 
$Type = 0 

$objWMI = [wmiClass] 'Win32_share' 
$objWMI.create($FolderPath, $ShareName, $Type) 

Start-Sleep -s 60 
Get-WmiObject -Class Win32_Share -Filter "Name='proxy'" | Remove-WmiObject 

Start-Sleep -s 60 
$objWMI = [wmiClass] 'Win32_share' 
$objWMI.create($FolderPath, $ShareName, $Type) 

但它不是循环。它在文件结束后停止。

回答

0

只需插入你的代码do {} while(true)块(或变更的条件,你需要什么)

+0

非常感谢您的帮助 – user1097593 2011-12-14 13:12:37

0

喜欢的东西:

$done = $false 
while(!$done) 
{ 
    $FolderPath = "D:\proxy" 
$ShareName = "proxy" 
$Type = 0 

$objWMI = [wmiClass] 'Win32_share' 
$objWMI.create($FolderPath, $ShareName, $Type) 

Start-Sleep -s 60 
Get-WmiObject -Class Win32_Share -Filter "Name='proxy'" | Remove-WmiObject 

Start-Sleep -s 60 
$objWMI = [wmiClass] 'Win32_share' 
$objWMI.create($FolderPath, $ShareName, $Type) 
} 
1

你并不需要最后的创建步骤,在一个循环中它会在第一个循环步骤中创建。要取消操作,请按CTRL + C。

$FolderPath = "D:\proxy" 
$ShareName = "proxy" 
$Type = 0 

while($true){ 
    #create the share 
    $objWMI = [wmiClass] 'Win32_share' 
    $objWMI.create($FolderPath, $ShareName, $Type) 

    # remove it after 5 minutes 
    Start-Sleep -s 300 
    Get-WmiObject -Class Win32_Share -Filter "Name='proxy'" | Remove-WmiObject 

    # wait one minute, share will be created in next loop iteration 
    Start-Sleep -s 60 
} 
+0

完全适合我。 – 2014-01-29 20:14:06