0

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.

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly for me.
0

Simply insert your code to do {} while(true) block (or change the condition to whatever you need)

Comments

0

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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.