How to properly pause a script

This topic contains 6 replies, has 3 voices, and was last updated by  Jon 1 month ago.

  • Author
    Posts
  • #99163

    Jon
    Participant

    This has got to be a simple question, but I can't seem to get this right.

    I have a script that copies photos from one share to my local machine, and then will upload them to AD and Azure. Often the photos are not named properly so I want to pause the script for a moment to review the names and then continue on if good or once I have manually gone in an edited the broken names. I've tried start-sleep, pause, read-host and no matter which one I use the pause happens before the list of the names.

    #variables
    $source = "\\server\photos"
    $dest = "C:\Scripts\Photos\"
    $oldphotos = "C:\scripts\Photos\old\"
    
    #copy files from share to dest
    
    Get-Childitem $source | Copy-Item -Destination $dest
    
    #prompt for manual review of files to ensure they are in the proper format
    
    Get-ChildItem $dest *.jpg | select name
    
    Read-Host "Review names and make sure they are correct"
    
    
  • #99165

    Don Jones
    Keymaster

    The only way to pause-and-review would be to "break" the script. Start-Sleep just initiates a countdown and then proceeds.

    Try adding a Write-Debug, with a message of some kind. When you run the script, that should produce a prompt. You can [S]uspend, review the contents of a variable or what's on the screen, and run "Exit" to resume.

    It'd be better to capture the output of your command into a variable than just letting it dump to the screen. That'll make it easier to review.

  • #99168

    Jon
    Participant

    I couldn't get write-debug to work, but I did get write-warning to work. Had to convert the output to a string though.

    As always, thanks Don!

    $output = Get-ChildItem *.jpg | select name | out-string
    Write-Warning -Message "Check file names and correct if needed, otherwise hit confirm to continue $output" -WarningAction Inquire
    
  • #99172

    Joel Sallow
    Participant

    You can avoid the Out-String by doing:

    $output = Get-ChildItem -Include '*.jpg' | Select-Object -ExpandProperty Name
    # short version with aliases:
    $output = Get-ChildItem '*.jpg' | Select -Expand Name
  • #99175

    Jon
    Participant

    You are correct, however when doing expandproperty or dotting it out to name makes the format look bad

    WARNING: Check file names and correct if needed, otherwise hit confirm to continue name.jpg name2.jpg name3.jpg name4.jpg name5.jpg

    vs

    WARNING: Check file names and correct if needed, otherwise hit confirm to continue
    Name
    —-
    name.jpg
    name2.jpg
    name3.jpg
    name4.jpg

  • #99181

    Joel Sallow
    Participant

    Ah, I see! Yep, in that case, nice find. I tend not to use a lot of Out-String myself. 🙂

  • #99184

    Jon
    Participant

    Neither do I, but it kept barking at me about not being a string 🙂

You must be logged in to reply to this topic.