PowerShell: how to rerun or close a scirpt after executed?

Kadhim, Mohammed 1 Reputation point
2022-05-06T01:35:35.913+00:00

Hello,

I was wondering how can I rerun or close this script? tried multiple ways but keep on failing :/. Your help is greatly appreciated

$CredAuth = "No"

Do {

$Cred = $null
$Domain = $null
$Root = $null
$userName = $null
$Password = $null

$Cred = $Host.UI.PromptForCredential("Need Credentials", "Please enter your A- Account username and password.    Example: domain\a-jdoe", "","NetBiosUserName")

if(!$Cred)
{
    return
}



Try
{
    $userName = $Cred.username
    $Password = $Cred.GetNetworkCredential().password

    $Root = "LDAP://" + ([ADSI]'').DistinguishedName
    $Domain = New-Object System.DirectoryServices.DirectoryEntry($Root,$Username,$Password)
}
Catch
{
    Continue
}

If(!$Domain)
{

}
Else
{
    If ($domain.Name -ne $null)
    {
        $CredAuth ="Yes"
    }

}

} While ($CredAuth -eq "No")

$ComputerName = Read-Host -Prompt 'Computer Name to Process'
if ($ComputerName) {
Write-Host "We can now use the computer name [$ComputerName] in our code"
} else {
Write-Warning -Message "No Compouter Name Input"
}

  foreach ($computer in $ComputerName.Split(" "))

{

    if (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue)

    {

        Write-host ("About to clean : "+$computer)

$disk = ([wmi]"\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")

"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - Before Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output

Write-output "Cleaning is in progress, Please wait"

Remove-Item -Force -erroraction 'silentlycontinue' \$ComputerName\c$\Windows\Temp* -Recurse

Remove-Item -Force \$computername\c$\windows\ccmcache* -Recurse

Remove-Item -Force \$computername\C$\$Recycle.Bin*

Invoke-Command -computername $computername -ScriptBlock {Clear-RecycleBin -DriveLetter C -force -ErrorAction SilentlyContinue}

$disk = ([wmi]"\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")

"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - After Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
""
"_________________________________________________________________"

}
}

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,534 questions
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2022-05-06T05:39:01.747+00:00

    Hello @Anonymous ,
    I just tried running in a separate CMD window and it went past.

    1. Open the command window in Admin Mode
    2. Browse to the path where the script is placed
    3. Run the script

    199420-image.png

    0 comments No comments

  2. Limitless Technology 39,926 Reputation points
    2022-05-11T07:11:06.283+00:00

    Hello

    Thank you for your question and reaching out.
    I can understand you are having some issues \ queries related to rerun or close a scripts after executed.

    Using Exit will exit PowerShell too. If you wish to "break" out of just the current function or script - use Break.
    For Example :


    mytest.ps1

    Write-Host " Please Read this before the PowerShell session closes!!"
    Start-Sleep -Seconds 10
    Exit



    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.