Script doing weird stuff with imported CSV

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Greg Dent Greg Dent 8 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33540
    Profile photo of Greg Dent
    Greg Dent
    Participant

    Hi all, been a while.

    I've been writing a script, which imports a CSV containing usernames, file names, and some other info. It's an export of some user data I ran using another powershell scriptlet. It has 3 headers: SamAccountName, ScriptFile and HomePath.

    All I'm wanting to do is to create a script to enumerate the CSV, look at the ScriptFile column, and do some stuff with it. The stuff consists of building some full paths to actual data (taken from the ScriptFile column for each user account) using Join-Path, printing out the final vars to make sure they are correct, then passing the completed string through to robocopy for some file move action. What I get is a looping output of the CSV file and NOTHING else.

    I thought this would be easy, and I swear to god I've done this before using this method, and this stuff changes each time they update powershell... This is what I have so far...

    # Import Data
    Import-Csv G:\Scripts\homedrvs\homedrvs.csv
    foreach ($user in $file) {
    
    # Set vars
    $ScriptFile=$user.ScriptPath
    $SYSVOL="\\server\e$\Windows\SYSVOL\domain\scripts"
    $ScriptPath=Join-Path -Path $SYSVOL -ChildPath $ScriptFile
    $DestParentPath="G:\Scripts\homedrvs"
    $DestPath=Join-Path -Path $DestParentPath -ChildPath $ScriptFile
    
    # Test vars
    Write-Host "`nFile source: $ScriptPath"
    Write-Host "`nDestination: $DestPath"
    
    # Perform copy script
    #robocopy $ScriptPath $DestPath /e /r:3 /w:1 /tee /np /log+:G:\scripts\homedrvs\copylog.txt
    }
    

    As stated, all this does is spam my shell window with the entire contents of the CSV, and then loops round again for EACH line contained within the CSV. It's ignoring the actual script – my brain cant work out the logic on this one!!

    Any help appreciated.

    Edit: This is with the actual robocopy command hashed out (as you see in the script block), so all I want to do is make sure the variables are correct before un-hashing it 🙂

    #33542
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well, at the moment your Import-CSV isn't going into the $file variable like I suspect is your intent. You're not looping the CSV file, you're just dumping it into the pipeline.

    Oh, and Write-Host. Really? :).

    #33543
    Profile photo of Greg Dent
    Greg Dent
    Participant

    Aaaaargh... nice one. I cant believe I forgot that!!!

    Fixed:

    # Import Data
    $csv=Import-Csv "G:\Scripts\homedrvs\homedrvs.csv"
    foreach ($user in $csv) {
    
    # Set vars
    $ScriptFile=$user.ScriptPath
    $SYSVOL="\\server\e$\Windows\SYSVOL\domain\scripts"
    $ScriptPath=Join-Path -Path $SYSVOL -ChildPath $ScriptFile
    $DestParentPath="G:\Scripts\homedrvs"
    $DestPath=Join-Path -Path $DestParentPath -ChildPath $ScriptFile
    
    # Test vars
    Write-Host "`nFile source: $ScriptPath"
    Write-Host "`nDestination: $DestPath"
    
    # Perform copy script
    #robocopy $ScriptPath $DestPath /e /r:3 /w:1 /tee /np /log+:G:\scripts\homedrvs\copylog.txt
    }
    

    Also, whats wrong with Write-Host? 🙂

    #33626
    Profile photo of Greg Dent
    Greg Dent
    Participant

    I also had to fix the robocopy part, as I realised (thankfully before executing) that it will have failed or done something far more stupid.

    Working version if anyone cared 😉

    # Import Data
    $csv=Import-Csv "G:\Scripts\homedrvs\homedrvs.csv"
    foreach ($user in $csv) {
    
    # Set vars
    $ScriptFile=$user.ScriptPath
    $SYSVOL="\\server\e$\Windows\SYSVOL\domain\scripts"
    $ScriptPath=Join-Path -Path $SYSVOL -ChildPath $ScriptFile
    $DestParentPath="G:\Scripts\homedrvs"
    $DestPath=Join-Path -Path $DestParentPath -ChildPath $ScriptFile
    
    # Test vars
    Write-Host "`nFile source: $ScriptPath"
    Write-Host "`nDestination: $DestPath"
    
    # Perform copy script
    robocopy $SYSVOL $DestParentPath $ScriptFile /r:3 /w:1 /tee /np /log+:G:\scripts\homedrvs\copylog.txt
    }
    

    This is what happens when you haven't touched powershell for a few months! 😛

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.