Author Posts

January 1, 2012 at 12:00 am

by spundae at 2013-01-09 00:21:58

Hello,
I have a problem with a script that must mapped a huge amount of different drives, with alternate credentials that must take pipeline input for the root. Basically I want to get a textfile with paths take should change the root of the mapped drive, do some stuff and then disconnect the drive and continue with the next root path. Here is my script:

Function MSDOwnersFiles {
< #
.SYNOPSIS
Retrieves an overview of different shares and their respective owners
.DESCRIPTION
Get-MSDOwnersFiles uses new-psdrives with alternate credentials and maps these drives
with powershell
.PARAMETER LogErrors
Specify this switch to create a text log file of Paths that have a problem and
that could not be queried.
.PARAMETER ErrorLog
When used with -LogErrors, specifies the file path and name
to which failed computer names will be written. Defaults to
C:\Retry.txt.
.EXAMPLE
Get-Content names.txt | MSDOwnersFiles
.EXAMPLE
MSDOwnersFiles \\fileserver01\share01
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="Type a share")]
[ValidateCount(1,1500)]
[Alias('Path')]
[string[]]$root,
[string]$ErrorLog = 'c:\retry.txt',
[switch]$LogErrors
)
BEGIN {
Write-Verbose "getting the credentials to map the drive"
$cred= (Get-Credential)
Write-Verbose "Error log will be $ErrorLog"
# PowerShell Checks If a File Exists
$WantFile1 = "C:\retry.txt"
$wantFile2 = "c:\temp\samNameD50-2-fill-result.txt"
$FileExists = Test-Path $WantFile1
If ($FileExists -eq $True) {
Write-Verbose "deleting file $wantfile1"
Remove-Item C:\retry.txt
Write-Verbose "file retry.txt is deleted, continue"
}
Else {Write-Verbose "No log file in C so create one"}
Write-Verbose "Check for file $wantFile2"
# PowerShell Checks If a File Exists
$FileExists = Test-Path $WantFile2
If ($FileExists -eq $True) {
Write-Verbose "deleting file $wantfile2"
Remove-Item "C:\temp\samNameD50-2-fill-result.txt"
Write-Verbose "file $wantfile2 is deleted, continue"
}
Else {Write-Verbose "No log file in C so create one"}

}
PROCESS {
Write-Verbose "Beginning PROCESS block"
foreach ($path in $root) {
Write-Verbose "Querying $path"
[system.string]$letter='T'
Write-Verbose "The drive letter is $letter"
$root=($path | out-string)
New-PSDrive -Name $letter -PSProvider FileSystem -Root $root -Credential $cred -Scope global -Persist

Set-Location T:

$owners = Get-ChildItem T: -Recurse | get-acl | Group-Object Owner
Write-Output $Root | out-file c:\temp\owners-test.txt -append
Write-Output $owners | out-file c:\temp\owners-test.txt -append
Cd C:
get-PSDrive -Name $letter| Remove-PSDrive
}
Catch {
if ($LogErrors) {
$Root | Out-File $ErrorLog -Append
}
}
}
END {}
}
import-csv C:\temp\filepathscheck.csv |MSDOwnersFiles -LogErrors -Verbose | Format-Table -AutoSize | Out-File C:\temp\samNameD50-2-fill-result.txt -Append


It does not work the way I want but perhaps somebody can take a look and see what I am trying to achieve. Thank you in advance.

by DonJ at 2013-01-09 11:23:09

Unfortunately, I've no idea what you're trying to achieve. It's a little hard to pick out what you're trying to do based on code that you say doesn't work :).

Perhaps you could provide a simple, numbered list of the procedure you're trying to accomplish?

1. Disconnect a mapped drive
2. Connect a mapped drive
3. Do something with the mapped drive

Etc. Then, let us know which step you aren't able to make happen.

I'll point out one problem you have, which is that you've enabled ValueFromPipelineBypropertyName, but ByPropertyName isn't what you're doing. Based on the examples in your comments, you should use ValueFromPipeline instead.

by spundae at 2013-01-10 01:10:40

Hello Don,

Sorry that it is not clear, Let me explain it to you. What I want to achieve is that I have a textfile or csv that has paths in it. I am working on a machine that does not have rights on the fileservers so I want to use alternate credentials for that. I want to connect and then do some get-childitems, get the owners and group by those owners, output it into a text file and then, disconnect the mapped drive and continue with the other shared folder on the fileserver.
So basically it would need to pipe in the paths from a text file or csv and take that as a variable, the map the network drive with alternate credentials and with the next path as the root, do something on that drive and move on to the next path, root.
It works when I manually map the drive interactively but not scriptwise. When I do a new-psdrive with a root I get that drive but when I script/automate it, it will not map the drive, since new-psdrive root parameter takes input ByPropertyName I thought that it will make it work that way.
Still a noob but I want to use the function a bit advanced which I took from several books. 🙂 Sorry it was not clear

by DonJ at 2013-01-10 08:08:27

So what, in your current script, isn't working? If you CSV file contains a column named "root," it seems like it should work.