1

I have a script that will send items to the recycle bin (if selected) or delete items permanently. If the script is run locally, the recycle piece works properly.

However, if it's run from a different computer - in this case, my local machine runs the script against a shared folder on a server - the delete is permanent, and doesn't get sent to the recycle bin. The script (in a prior run) makes a decision about WHAT to delete by first setting the Archive bit to TRUE and then (after seeing how many backups it is to retain) un-setting the Archive bit for items to be deleted on the next execution of that same script.

My thought was to alter the main script to mark the files for deletion, but only do the physical action of deleting the file(s) only when the script was being run locally, or to put the Recycle script (by itself) as a Task on the server that would delete & send the item to the Recycle Bin that would run at a set interval.

My questions-

  1. In Powershell (using 2.0) how do you determine the source computer vs the target computer? In this case, the script is being run from MyPC, and it's target is Server1.
  2. The script will run whether the target is a mapped drive (Drive Y:), or if it's targeted by the servername (\Server1). How can you distinguish the above question in both of these cases?

2 Answers 2

1
  1. You can get the local computer name with $env:COMPUTERNAME. Use it to compare the value against the target server name.

  2. For each file, you'd have to check first if the drive is a mapped drive, if it is, get the server name from the wmi instance and compare it to $env:COMPUTERNAME.

You can get a file's Drive qualifier with the Split-Path cmdlet:

PS> $drive = Split-Path Q:\test.txt -Qualifier
PS> $drive
Q:

And then get the server name with WMI:

PS> (gwmi win32_logicaldisk -filter "drivetype=4 and deviceid='$drive'").ProviderName.Split('\')[2]    
Server1
4
  • Thanks. Since (in my case) the server name may also have a file or folder path as part of it, how do you determine the server part of \\Server1\filepath1\filepath2\test.txt? The above works fine if it's mapped.
    – steve_o
    Apr 30, 2012 at 13:50
  • You can use string parsing or regex. Here's a method that doesn't involve both methods: ([System.Uri]"\\Server1\filepath1\filepath2\test.txt").Host
    – Shay Levy
    Apr 30, 2012 at 14:00
  • I upvoted for a neat piece of code but I am wondering how to programmatically determine if the drive is a mapped drive? Empirically, it looks like (gwmi win32_logicaldisk -filter "drivetype=4 and deviceid='C:'") -eq $null will work but is there something simpler? Jun 12, 2012 at 3:13
  • Try the Win32_MappedLogicalDisk class. One gotcha though, mapped network drives are not visible from elevated process.
    – Shay Levy
    Jun 12, 2012 at 9:04
0

The OP wrote:

@Shay - Thanks for your help. I've learned a great deal from many posts by you on various Powershell sites.

I was able to use almost everything you suggested, and only had to add an extra line of code to make it work. I checked the property ([System.Uri]$markedFile).IsUnc to determine if the filename I've read is a UNC name.

It returns False if the drive is mapped, and True if it is UNC. From that, I'm able to get the servername & make a comparison to the environment. Code follows.

$markedFile = "\\Server1\foldername1\Error.log"
#$markedFile = "Y:\foldername1\Error.log"

$TargetComputer = $null
$thisComputer = Get-Content env:computername
if (Test-Path $markedFile) {  # if file exists
    if (([System.Uri]$markedFile).IsUnc) { # if it's a UNC name & not a mapped drive name
        $TargetComputer = ([System.Uri]$markedFile).Host
        }
    else {  #file is not a UNC name, it must be a mapped drive 
        $drive = Split-Path $markedFile -Qualifier
        $TargetComputer = (gwmi win32_logicaldisk -Filter "drivetype=4 and deviceid = '$drive'").Providername.split('\')[2]
    }
}

The above code works either way. Thank you again for your help!

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.