Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

powershell script how to add test network drive is present

hello, i have a script i use with gpo when user open windows session, thi script copy file from the network drive to the user profile.

but the script is executed every time before the netwotk drive "J:" is mounted.

any possibility to take script test that the network drive is mounted before begin a copy?

$Source = "j:\Appdata\Test\test1"
$Destination = "$env:USERPROFILE\AppData\ROAMING\Test\test1"

# Ensure we have exactlty the right case for source or it'll break the substitution operations below, Replace is case sensitive.
$Source = (Get-Item $Source).FullName
Get-ChildItem $Source -Recurse -File | ForEach-Object {
    # Create an intermediate object denoting the source and destination for each file
    [PSCustomObject]@{
        SourcePath      = $_.FullName
        SourceWriteTime = $_.LastWriteTime
        DestinationPath = Join-Path $Destination $_.FullName.Replace($Source, '')
    }
} | Where-Object {
    -not (Test-Path $_.DestinationPath) -or
    $_.SourceWriteTime -gt (Get-Item $_.DestinationPath).LastWriteTime
} | ForEach-Object {
    $ParentDirectory = Split-Path $_.DestinationPath -Parent

    if (-not (Test-Path $ParentDirectory)) {
        $null = New-Item $ParentDirectory -ItemType Directory
    }

    Copy-Item $_.SourcePath $_.DestinationPath -Force
}

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Testing something exists first is easy enough:
if (Test-Path J:) {

Open in new window

Adding that would mean it never really runs though.

Perhaps we can make it wait for the drive:
do {
   Start-Sleep -Seconds 5
} until (Test-Path J:)

Open in new window

The rest of the script follows exactly as it is now.

It would be simple to add a wait timeout as well should you need to. Otherwise that little loop will run until the user logs off again.
$Timeout = 30
$i = 0
do {
   Start-Sleep -Seconds 5
   $i++
} until ((Test-Path J:) -or ($i * 5) -ge $Timeout)

Open in new window

Chris
Avatar of cawasaki
cawasaki

ASKER

hello chris, so what i use just this:

do {
   Start-Sleep -Seconds 5
} until (Test-Path J:)
That would be a good start, yes.

Adding it in makes the whole thing into this:
# Wait for J to be mapped
do {
   Start-Sleep -Seconds 5
} until (Test-Path J:)

$Source = "j:\Appdata\Test\test1"
# One last check to make sure the source is available
if (Test-Path $Source) {
    $Destination = "$env:USERPROFILE\AppData\ROAMING\Test\test1"

    # Ensure we have exactlty the right case for source or it'll break the substitution operations below, Replace is case sensitive.
    $Source = (Get-Item $Source).FullName
    Get-ChildItem $Source -Recurse -File | ForEach-Object {
        # Create an intermediate object denoting the source and destination for each file
        [PSCustomObject]@{
            SourcePath      = $_.FullName
            SourceWriteTime = $_.LastWriteTime
            DestinationPath = Join-Path $Destination $_.FullName.Replace($Source, '')
        }
    } | Where-Object {
        -not (Test-Path $_.DestinationPath) -or
        $_.SourceWriteTime -gt (Get-Item $_.DestinationPath).LastWriteTime
    } | ForEach-Object {
        $ParentDirectory = Split-Path $_.DestinationPath -Parent

        if (-not (Test-Path $ParentDirectory)) {
            $null = New-Item $ParentDirectory -ItemType Directory
        }

        Copy-Item $_.SourcePath $_.DestinationPath -Force
    }
}

Open in new window

Chris
hello chris,

the script not work when user open session and i dont now why

but script work when user execute it manually
Is it running through group policy? I wonder if that's killing it. Let's have some simple logging. I'll make it write a log file to $env:TEMP as PowerShell.log, which will be "C:\Users\<Someone>\AppData\Local\Temp" depending a bit on the operating system.
$LogFile = "$env:Temp\PowerShell.log"

"Started $(Get-Date)" | Out-File $LogFile -Append

# Wait for J to be mapped
do {
   "Waiting for the J: drive to map" | Out-File $LogFile -Append
   Start-Sleep -Seconds 5
} until (Test-Path J:)

"Testing for existence of $Source" | Out-File $LogFile -Append

$Source = "j:\Appdata\Test\test1"
# One last check to make sure the source is available
if (Test-Path $Source) {
    "Source exists, starting copy process" | Out-File $LogFile -Append

    $Destination = "$env:USERPROFILE\AppData\ROAMING\Test\test1"

    # Ensure we have exactlty the right case for source or it'll break the substitution operations below, Replace is case sensitive.
    $Source = (Get-Item $Source).FullName
    Get-ChildItem $Source -Recurse -File | ForEach-Object {
        # Create an intermediate object denoting the source and destination for each file
        [PSCustomObject]@{
            SourcePath      = $_.FullName
            SourceWriteTime = $_.LastWriteTime
            DestinationPath = Join-Path $Destination $_.FullName.Replace($Source, '')
        }
    } | Where-Object {
        -not (Test-Path $_.DestinationPath) -or
        $_.SourceWriteTime -gt (Get-Item $_.DestinationPath).LastWriteTime
    } | ForEach-Object {
        $ParentDirectory = Split-Path $_.DestinationPath -Parent

        if (-not (Test-Path $ParentDirectory)) {
            "Creating folder $ParentDirectory" | Out-File $LogFile -Append

            $null = New-Item $ParentDirectory -ItemType Directory
        }

        "Copying $($_.SourcePath) to $($_.DestinationPath)" | Out-File $LogFile -Append

        Copy-Item $_.SourcePath $_.DestinationPath -Force
    }
}

"Complete" | Out-File $LogFile -Append

Open in new window

Chris
yes its a gpo
Ah well, give the logging version a try. If it creates the log file it'll be interesting to see how far it got.

Chris
this script not work for windows 7 machine seems to work only on windows 8

its like the last time :)
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$env:Temp not work i think

i have change it to c:\temp
script manually work but with gpo nothing happen and no log file
we have an other gpo with other logon script may be this is the problem?
I don't think so, but I'm not sure to be honest, it's been a while (quite a lot of years) since I've played around with it.

The other script is also PowerShell? That is, it's not a general problem running PS scripts at logon is it?

Chris
no because the other script for close windows session work
Hmm that's a pain.

We could try starting a transcript log in there, but if it won't even attempt to write to the log file I doubt it'll do any good.

Chris
i have disable all other gpo and now it work so the problem came from other gpo and its ok i will found other way.

thanks for help Chris..
Oh good :) Well as long as that's the problem :)

Chris
ah now i need to remove log part rom script