简体   繁体   English

Powershell UNC 路径与凭据

[英]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.背景:我们创建了一个脚本来观察文件的时间戳,当它检测到热文件夹中持续时间长达 10 分钟的文件时,脚本会自动重启服务。

Thats actually the powershell working powershell script:这实际上是 powershell 工作 powershell 脚本:

#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:现在我需要使用正确的凭据连接我的 powershell 脚本,我尝试过的一些示例可以在这里看到:

#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!谢谢!

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. Get-ChildItem没有-credential参数。

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.您可以先尝试创建映射驱动器或路径,但您需要建立凭证 object 才能与New-PSDrive命令一起使用。

$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:注意对于 Powershell 4.0,更改:

$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 .您可以在此处阅读有关 New-PSDrive 的信息。

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 .这是第一个Google 结果 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.然后使用它来创建凭据 object,如上所示。

Note: The file can only be decoded on the computer and under the account from which it was created.注意:该文件只能在计算机上和创建它的帐户下解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM