我正在为用户编写一个打开文件的解决方案,这个文件应该导航到某个网站并将用户的用户名插入到他们的登录表单中。 此文件需要由 citrix session 中的用户访问。
这应该非常简单,我发现了一种通过 Powershell 实现的方法:
$aduser = Get-ADUser $env:USERNAME -Properties EmailAddress
$emailaddress = $aduser.EmailAddress
$url = "https://website.org/loginpage.asp"
$ie = New-Object -comobject "InternetExplorer.Application"
$ie.visible = $true
$ie.Navigate($url)
WaitForPage 10
$ie.Document.GetElementById("USERID").Value = $emailaddress
这非常有效 - 它打开网页,并插入用户名(电子邮件地址)。
但是,当用户从他们的机器上运行它时,似乎不可能隐藏 CMD 窗口(如果从 .cmd
或 .bat
运行)以及Powershell 窗口。 -WindowStyle Hidden
只是减少了窗口出现的时间长度 - 这是 Not Acceptable 解决方案。
所以我的下一个行动计划是在 c# 中重新创建上述代码并将其作为 exe 分发(因为这不太可能显示任何控制台窗口)。但是,我似乎无法在不依赖外部库的 C# 中找到任何执行此操作的方法(例如 Selenium,它还需要安装驱动程序,这对我来说不是一个有效的选项)。
我想我的问题是 - 可以在 C# 中重新创建上述 Powershell 脚本吗?该脚本中的 -comobject
是 .NET 对象吗?如果是,我如何在 C# 中利用它?
供引用 - 我目前调用 .ps1
文件如下(在 CMD 文件中):
START Powershell.exe -WindowStyle Hidden -File \\file\Folder\SK\scripts\powershell\opensite.ps1
而且我还没有找到任何方法来实际隐藏出现的控制台窗口。我要么需要为此找到解决方案,要么需要一种在 C# 中实现相同功能的简单方法。
最佳答案
作为我commented above ,您可以通过 wscript.exe
使用 VBScript 或 Jscript 来避免启动另一个控制台窗口。这是一个示例 Jscript 脚本,编写为批处理 + Jscript 混合。用 .bat 扩展名保存它,加盐调味,然后试一试。
@if (@CodeSection == @Batch) @then
@echo off & setlocal
wscript /e:JScript "%~f0"
goto :EOF
@end // end batch / begin JScript hybrid code
var user = WSH.CreateObject("ADSystemInfo"),
email = GetObject("LDAP://" + user.UserName).EmailAddress,
url = "https://website.org/loginpage.asp",
ie = WSH.CreateObject('InternetExplorer.Application');
ie.visible = true;
ie.Navigate(url);
while (ie.readyState != 4) WSH.Sleep(25);
ie.document.getElementById('USERID').value = email;
if (ie.document.getElementById('password'))
ie.document.getElementById('password').focus();
它实际上是一个多语言脚本,因为您可以使用 .bat 扩展名或 .js 保存它。作为 .js,您可以双击它并启动(假设 .js 文件与 wscript.exe
相关联,因为它们通常默认情况下是这样),根本没有任何控制台窗口。
如果您更喜欢 .exe 文件,可以很容易地将上面的脚本修改为一个可以自编译和链接 Jscript.NET 可执行文件的脚本。 (这个脚本仍然有一个 .bat 扩展名。)
@if (@CodeSection == @Batch) @then
@echo off & setlocal
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*jsc.exe"') do (
if not exist "%~dpn0.exe" "%%~I" /nologo /target:winexe /out:"%~dpn0.exe" "%~f0"
)
"%~dpn0.exe"
goto :EOF
@end // end batch / begin JScript.NET hybrid code
import System;
import System.Diagnostics;
try {
var wshShell:Object = new ActiveXObject("Wscript.Shell"),
user:Object = new ActiveXObject("ADSystemInfo"),
email:String = GetObject("LDAP://" + user.UserName).EmailAddress,
url:String = "https://website.org/loginpage.asp",
ie:Object = new ActiveXObject('InternetExplorer.Application');
}
catch(e:Exception) { System.Environment.Exit(1); }
ie.visible = true;
ie.Navigate(url);
// force IE window to the foreground and give it focus
var proc:System.Diagnostics.Process[] = System.Diagnostics.Process.GetProcesses();
for (var i:Int16 = proc.length, hwnd:IntPtr = IntPtr(ie.hwnd); i--;) {
if (proc[i].MainWindowHandle === hwnd && wshShell.AppActivate(proc[i].Id)) break;
}
while (ie.readyState != 4) System.Threading.Thread.Sleep(25);
ie.document.getElementById('USERID').value = email;
if (ie.document.getElementById('password'))
ie.document.getElementById('password').focus();
关于c# - 没有控制台窗口或依赖项的简单 DOM 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37052555/