【发布时间】:2021-09-24 13:29:52
【问题描述】:
我正在尝试创建一个脚本以通过网络获取文件夹的内容。我注意到 Get-Childitem 在列表中的每个其他 IP 地址上都会抛出“拒绝访问”错误。
例如我这样列出IP:
- IP1
- IP2
- IP3
- IP4
Get-ChildItem 适用于 IP1 和 IP3,但返回 IP2 和 IP4 的访问被拒绝错误。仅当我在以 SYSTEM 身份运行的控制台上运行脚本时才会发生这种情况。 编辑:我创建此脚本的远程工具只能作为系统运行控制台
谁能告诉我代码中是否有任何可能导致此问题的内容?
#Set Variables
$pw = Read-Host -AsSecureString "Enter password"
$usrname = 'username'
$folderpath = 'C$\Folder\Subfolder1\Subfolder'
#Loop
foreach ($ipaddress in Get-Content -Path .\DeviceIPList.txt) {
$credential = New-Object System.Management.Automation.PsCredential("$ipaddress\$usrname",$pw)
Try {
if ($(Test-Path drv:) -eq 'True') {
Remove-PSDrive "drv"
} else {
New-PSDrive -Name "drv" -PSProvider FileSystem -Root "\\$ipaddress\C$" -Credential $credential -ErrorAction Stop | Out-Null
}
$vhdfile = Get-ChildItem -path "\\$ipaddress\$folderpath" -ErrorAction Stop
Write-Host -ForegroundColor Green "$ipaddress,Found $vhdfile in $($folderpath.Replace('$',':'))"
Write-Output "$ipaddress,Found $vhdfile in $($folderpath.Replace('$',':'))" | Out-File -Append .\Report.txt
}
Catch [System.ComponentModel.Win32Exception] {
Write-Host -ForegroundColor Cyan "$ipaddress,$_"
Write-Output "$ipaddress,$_" | Out-File -Append .\Report.txt
}
Catch {
Write-Host -ForegroundColor Yellow "$ipaddress,$_"
Write-Output "$ipaddress,$_" | Out-File -Append .\Report.txt
}
Finally {
$error.Clear()
Start-Sleep -Seconds 2
net use \\$ipaddress\$folderpath /d 2>&1>$null
}
}
【问题讨论】:
-
系统账号用于本地系统,一般不能使用系统账号连接远程机器。
-
抱歉,我已更新帖子以进行澄清。不幸的是,我使用的工具只能将控制台作为系统运行。有没有办法让它工作?