Reputation: 11
I'd like to loop to commands with powershell
for creating local share and remove it after 5 minutes. Then wait 1 minute and create the share again.. Then after 5 minutes to remove it and after another one minute to create it again and so on and so forth ..
Got those two:
$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)
But it's not looping. It stops after end of file.
Upvotes: 0
Views: 7186
Reputation: 126712
You don't need the last creation step, in a loop it will be created in the first loop step. To cancel the operation, press 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
}
Upvotes: 1
Reputation: 13483
Something like:
$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)
}
Upvotes: 0
Reputation: 5512
Simply insert your code to do {} while(true)
block (or change the condition to whatever you need)
Upvotes: 0