l.jablonski
l.jablonski

Reputation: 71

Powershell UNC Path with credentials

Background: we created a script was watch our timestamp of the files and when it detect a file that last longer as 10 min in the hotfolder, the script automaticly restart the service.

Thats actually the powershell working powershell script:

#Variablen ausfüllen
$watchfolder = '\\dcsrv\public\Scanner-Eingang'#UNC Pfade funktionieren
$CurTimeStamp = Get-Date
$FileAge = 10 #in Minuten

#Er prüft die Zeit der Dokumente
$Files = Get-ChildItem -Path $watchfolder  | Where-Object{($_.LastWriteTime -le 
$CurTimeStamp.AddMinutes(-1 * $FileAge))}

#Für jedes Dokument
ForEach ($File in $Files) {
Write-Host " " $File " - " $File.LastWriteTime
## Damit wir der Dienst neugestartet
Restart-Service -Name Spooler

## Sobald er eine Datei gefunden hat stopt er
break
}

The service is running on a server out of the domain and need to reach the hotfolders. The hotfolders are on a domaincontroller with other credentials. Now i need to connect in my powershell script with the right credentials, some examples i tried you can see here:

#Variablen ausfüllen
$watchfolder = '\\dcsrv\public\Scanner-Eingang'#UNC Pfade funktionieren
$CurTimeStamp = Get-Date
$username = 'user'
$password = '1234'
$FileAge = 10 #in Minuten

#Er prüft die Zeit der Dokumente
$Files = Get-ChildItem -Path $watchfolder $username $password | Where-Object{($_.LastWriteTime -le 
$CurTimeStamp.AddMinutes(-1 * $FileAge))}

#Für jedes Dokument
ForEach ($File in $Files) {
Write-Host " " $File " - " $File.LastWriteTime
## Damit wir der Dienst neugestartet
Restart-Service -Name Spooler

## Sobald er eine Datei gefunden hat stopt er
break
}

didnt worked then i tried something else

#Variablen ausfüllen
$watchfolder = '\\dcsrv\public\Scanner-Eingang'#UNC Pfade funktionieren
$CurTimeStamp = Get-Date
$FileAge = 10 #in Minuten

#Er prüft die Zeit der Dokumente
$Files = Get-ChildItem -Path $watchfolder /user:user password:1234 | Where-Object{($_.LastWriteTime - 
le $CurTimeStamp.AddMinutes(-1 * $FileAge))}

#Für jedes Dokument
ForEach ($File in $Files) {
Write-Host " " $File " - " $File.LastWriteTime
## Damit wir der Dienst neugestartet
Restart-Service -Name Spooler

## Sobald er eine Datei gefunden hat stopt er
break
}

also without success

Now i hope somebody can help me.

Thanks you!

Upvotes: 2

Views: 5778

Answers (1)

Steven
Steven

Reputation: 7057

I think the main question is how do you authenticate to the remote domain, in order to watch the file share. Get-ChildItem doesn't have a -credential parameter.

You can try creating a mapped drive or path first, but you'll need to establish a credential object to use with the New-PSDrive command.

$Pwd  = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Cred = [System.Management.Automation.PSCredential]::New('UserName', $pwd)

New-PSDrive -Root '\\dcsrv\public\Scanner-Eingang' -PSProvider FileSystem -Name "ShareToMonitor" -Credential $Cred
...

Note For Powershell 4.0, change:

$Cred = [System.Management.Automation.PSCredential]::New('UserName', $pwd)

to:

$Cred = New-Object System.Management.Automation.PsCredential('UserName',$Pwd)

You can read about New-PSDrive here.

However, it's usually a bad idea to write a password directly into a script. You can store it securely in a file

There are many guides and/or blog articles on the Internet to help with that. Here's the first Google result. Once you establish the secure file you'll have to add code to the script to retrieve it. Then use it to create the credential object as demonstrated above.

Note: The file can only be decoded on the computer and under the account from which it was created.

Upvotes: 4

Related Questions