用于站点登录的 Powershell 脚本不起作用
Powershell Script for site login not working
我的总体目标是每天自动将 FTP 转移到 运行。我有一个 PowerShell 脚本,我想用它来登录到有问题的 FTP 站点。我知道该脚本有效,因为我已经用其他 sites/logins 对其进行了测试,并且每次都能完美运行。问题是当网站加载时,登录屏幕加载弹出 window(下图),我无法右键单击检查元素或 F12 来搜索 HTML 对象名称、元素ID 等。我已经尝试了几乎所有我能想到的猜测登录名称 windows,但我认为我的脚本甚至没有与弹出窗口通信 window。
关于如何解决这个特定问题的任何建议?我也包含了最后收到的错误消息。
这是我的脚本:
$username = "myuser";
$password = "mypass";
#Create an instance of IE
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
#Navigate to the site
$ie.Navigate("website.com")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 1;} #wait for browser idle
($ie.document.getElementById('User') | select -first 1).value = $username #enters username
Start-Sleep -Seconds 1
($ie.document.getElementById('Pass') | select -first 1).value = $password #enters password
Start-Sleep -Seconds 1
($ie.document.getElementById('Log on') | select -first 1).click() #clicks login button
Start-Sleep -Seconds 1
Screenshot of FTP Site
Screenshot of error messages
考虑使用 PSFTP Powershell Module 而不是 Internet Explorer COM 对象。使用以下命令安装它:
Install-Module PSFTP
基本上,使用Set-FTPConnection
打开到FTP服务器的会话,New-FTPItem
创建新的FTP目录,Add-FTPItem
上传新的文件。 Get-FTPItem
将用于检索远程文件。
以下是 TechNet Gallery site 中的一些示例:
Import-Module PSFTP
Set-FTPConnection -Credentials mgajda -Server ftp://ftp.server.org -Session MyTestSession -UsePassive
$Session = Get-FTPConnection -Session MyTestSession
New-FTPItem -Session $Session -Name TestRootDir
New-FTPItem -Session $Session -Name TestDir1 -Path /TestRootDir
New-FTPItem -Session $Session -Name TestDir2 -Path /TestRootDir
New-FTPItem -Session $Session -Name TestDir11 -Path /TestRootDir/TestDir1
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse -Depth 2
"Test File" | Out-File TestFile.txt
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir -Overwrite
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir/TestDir1
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir/TestDir2 -BufferSize 5
Add-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestDir11 -LocalPath TestFile.txt
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse -Depth 2
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse
Get-FTPItemSize -Session $Session -Path /TestRootDir/TestDir1/TestFile.txt
Rename-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestFile.txt -NewName TestFile2.txt
Rename-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestFile2.txt -NewName ../TestFile2.txt
Get-FTPChildItem -Session $Session -Path /TestRootDir | Get-FTPItem -Session $Session -LocalPath C:\test
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse | Get-FTPItem -Session $Session -LocalPath C:\test -RecreateFolders
Get-FTPChildItem -Session $Session -Path /TestRootDir -Filter TestF* | Remove-FTPItem -Session $Session
Remove-FTPItem -Session $Session -Path /TestRootDir -Recurse
建立凭证
有几种方法可以安全地构建凭据对象。 (通常)最好的方法是使用 Get-Credential
:
$cred = Get-Credential $user
但是,Get-Credential
将始终提示您输入密码。只要在需要解密凭据的同一用户的同一台机器上生成上述凭据,就可以将凭据对象存储在文件系统上,稍后再读入(作为同一台机器上的同一用户) 在其他会话中使用该凭据。
假设您如上所示创建了 $cred
:
# Export the credential object to a file on disk
$cred | Export-CliXml C:\path\to\secretAD.xml
# Import the credential object later on
$cred = Import-CliXml C:\path\to\secretAD.xml
Export-CliXml
和 Import-CliXml
是两个方便的 cmdlet,可让您将任何 Powershell 对象序列化到磁盘并稍后将其读回另一个 Powershell 会话。这有一些限制(例如,不要尝试使用以这种方式读取的 COM 对象)但它适用于存储和读取凭据对象的情况。
如果您想要一种更便携的方法来加密凭据中的密码,请参阅 the second example here。
我的总体目标是每天自动将 FTP 转移到 运行。我有一个 PowerShell 脚本,我想用它来登录到有问题的 FTP 站点。我知道该脚本有效,因为我已经用其他 sites/logins 对其进行了测试,并且每次都能完美运行。问题是当网站加载时,登录屏幕加载弹出 window(下图),我无法右键单击检查元素或 F12 来搜索 HTML 对象名称、元素ID 等。我已经尝试了几乎所有我能想到的猜测登录名称 windows,但我认为我的脚本甚至没有与弹出窗口通信 window。 关于如何解决这个特定问题的任何建议?我也包含了最后收到的错误消息。
这是我的脚本:
$username = "myuser";
$password = "mypass";
#Create an instance of IE
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
#Navigate to the site
$ie.Navigate("website.com")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 1;} #wait for browser idle
($ie.document.getElementById('User') | select -first 1).value = $username #enters username
Start-Sleep -Seconds 1
($ie.document.getElementById('Pass') | select -first 1).value = $password #enters password
Start-Sleep -Seconds 1
($ie.document.getElementById('Log on') | select -first 1).click() #clicks login button
Start-Sleep -Seconds 1
Screenshot of FTP Site
Screenshot of error messages
考虑使用 PSFTP Powershell Module 而不是 Internet Explorer COM 对象。使用以下命令安装它:
Install-Module PSFTP
基本上,使用Set-FTPConnection
打开到FTP服务器的会话,New-FTPItem
创建新的FTP目录,Add-FTPItem
上传新的文件。 Get-FTPItem
将用于检索远程文件。
以下是 TechNet Gallery site 中的一些示例:
Import-Module PSFTP
Set-FTPConnection -Credentials mgajda -Server ftp://ftp.server.org -Session MyTestSession -UsePassive
$Session = Get-FTPConnection -Session MyTestSession
New-FTPItem -Session $Session -Name TestRootDir
New-FTPItem -Session $Session -Name TestDir1 -Path /TestRootDir
New-FTPItem -Session $Session -Name TestDir2 -Path /TestRootDir
New-FTPItem -Session $Session -Name TestDir11 -Path /TestRootDir/TestDir1
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse -Depth 2
"Test File" | Out-File TestFile.txt
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir -Overwrite
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir/TestDir1
Get-ChildItem TestFile.txt | Add-FTPItem -Session $Session -Path /TestRootDir/TestDir2 -BufferSize 5
Add-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestDir11 -LocalPath TestFile.txt
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse -Depth 2
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse
Get-FTPItemSize -Session $Session -Path /TestRootDir/TestDir1/TestFile.txt
Rename-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestFile.txt -NewName TestFile2.txt
Rename-FTPItem -Session $Session -Path /TestRootDir/TestDir1/TestFile2.txt -NewName ../TestFile2.txt
Get-FTPChildItem -Session $Session -Path /TestRootDir | Get-FTPItem -Session $Session -LocalPath C:\test
Get-FTPChildItem -Session $Session -Path /TestRootDir -Recurse | Get-FTPItem -Session $Session -LocalPath C:\test -RecreateFolders
Get-FTPChildItem -Session $Session -Path /TestRootDir -Filter TestF* | Remove-FTPItem -Session $Session
Remove-FTPItem -Session $Session -Path /TestRootDir -Recurse
建立凭证
有几种方法可以安全地构建凭据对象。 (通常)最好的方法是使用 Get-Credential
:
$cred = Get-Credential $user
但是,Get-Credential
将始终提示您输入密码。只要在需要解密凭据的同一用户的同一台机器上生成上述凭据,就可以将凭据对象存储在文件系统上,稍后再读入(作为同一台机器上的同一用户) 在其他会话中使用该凭据。
假设您如上所示创建了 $cred
:
# Export the credential object to a file on disk
$cred | Export-CliXml C:\path\to\secretAD.xml
# Import the credential object later on
$cred = Import-CliXml C:\path\to\secretAD.xml
Export-CliXml
和 Import-CliXml
是两个方便的 cmdlet,可让您将任何 Powershell 对象序列化到磁盘并稍后将其读回另一个 Powershell 会话。这有一些限制(例如,不要尝试使用以这种方式读取的 COM 对象)但它适用于存储和读取凭据对象的情况。
如果您想要一种更便携的方法来加密凭据中的密码,请参阅 the second example here。