Reputation: 3881
I'm working on a script that uses get-childitem on the other server, but need to change it so it uses credentials of the local account on the other server to do that. When I was just using Active Directory to do that, I was saving the task in our scheduler with my AD login, and it was good on the other server, using the UNC path. But we decided to change it to the local login there recently and I'm getting an error message, trying to use net use. Does anyone know of a good way to do this with the UNC path instead? Or, any idea why the following is giving an error message?
function GetSecureLogin(){
$global:username = "stuff"
$global:password = get-content C:\filename.txt | convertto-securestring
}
function Cleanup([string]$Drive) {
try {
$deleteTime = -42
$now = Get-Date
**#this is saying cannot find path '\\name.na.xxx.net\20xServerBackup\V' name truncated**
Get-ChildItem -Path $Drive -Recurse -Force |Where-Object {$_.LastWriteTime -lt $limit} | Remove-Item -Force
}
Catch{
Write-Host "Failed"
}
}
#####################start of script####################
$share = '\\name.na.xxx.net\20xServerBackup\'
$TheDrive = '\\name.na.xxx.net\20xServerBackup\VMs\'
$global:password = ""
$global:username = ""
GetSecureLogin
net use $share $global:password /USER:$global:username
[array]$DriveArray = @(TheDrive)
try{
$i=0
for ($i = $DriveArray.GetLowerBound(0); $i -le $DriveArray.GetUpperBound(); $i++) {
$tempDrv = $DriveArray[$i]
Cleanup $tempDrv
}
}
catch [Exception] {
Write-Host $_.Exception.Message
}
As you can see, I started using the example at this link with net use, but it's not doing the trick to use credentials to access the other server. powershell unc path cred
Upvotes: 0
Views: 24464
Reputation: 87
About 9 years late on this answer, but I ran into the same issue with the command [get-childitem] lacking the -Credential parameter for remote execution. I'm using PowerShell 7.3.1, but I believe this workaround solution will work using PowerShell 2.0/3.0 if by chance you're still stuck in the past!
Description: Use the Invoke-command with the Credential and the Scriptblock parameters to execute commands on a remote computer. Capture the date and utilize the AddDays method to control the timeframe you're wanting to capture or execute against. This example only displays the files that meets the condition, but it can be updated to remove them.
invoke-command -ComputerName $computer -Credential $creds -ScriptBlock {
$date = get-date
$files = get-childitem c:\temp -Recurse
$newfiles = $files | where-object {$date.AddDays(-7) -lt $_.LastWriteTime}
foreach ($file in $newfiles){
write-host "$($file.Name) LastWriteTime: $($file.LastWriteTime)"
}
}
Upvotes: 0
Reputation: 3881
I got it to work this way, with New-PSDrive as @robert.westerlund suggests above:
$DestPath = split-path "$Drive" -Parent #this gives format without slash at and and makes powerShell *very happy*
New-PSDrive -Name target -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
$temp1 = Get-ChildItem -Path target:\VMs\ -Recurse -Force | Where-Object { $_.LastWriteTime -lt $limit}
Get-ChildItem -Path $Drive -Recurse -Force | Where-Object { $_.LastWriteTime -lt $limit} | Remove-Item -Force
Remove-PSDrive target
I had to add the cred part like this too:
$global:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $global:username, $global:password
Upvotes: 5