Power Shell脚本打开网络后打开和关闭网页

时间:2017-04-28 16:31:27

标签: powershell

Hello Stack社区我一直试图搜索一段时间,但似乎找不到任何东西。我想做的就是使用iexplorer.exe" www.myserverIP.com \ dev \ emailme.php"启动一个网页。假设是一种气隙类型的服务器,打开其连接需要备份多个其他服务器然后关闭其连接。在打开和关闭它之间我希望它访问我的wamp服务器运行一个PHP脚本,发送给我一个发生这种情况的电子邮件:D我已经制作了PHP脚本。任何其他想法都是受欢迎的,我总是喜欢尝试新的东西!

代码如下:

    # Monday Backup Script

#7zip path:
$path = "C:\Program Files\7-Zip\7z"
$date=get-date -format "%M.%d.yy"

#:: /COPYALL :: COPY ALL file info
#:: /B :: copy files in Backup mode. 
#:: /SEC :: copy files with SECurity
#:: /MIR :: MIRror a directory tree 

#:: /R:n :: number of Retries
#:: /W:n :: Wait time between retries
#:: /LOG :: Output log file
#:: /NFL :: No file logging
#:: /NDL :: No dir logging

# Open Network
netsh interface set interface ethernet enabled

# Alert for Backup

iexplore.exe "http://172.20.255.3/NeikoDev/Backup%20emails/Backup%20Emails.php"
taskkill /IM iexplore.exe /F
# ^^^ THIS IS THE CODE THAT DOESNT WORK FOR THIS ^^^^**



# Wait for network to come online
sleep 120

# Backup NAS to local
ROBOCOPY "\\10.0.5.100\PrivateData\." "D:\PrivateData\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\Monday.txt
ROBOCOPY "\\10.0.5.100\PrivateData1\." "D:\PrivateData1\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayPrivate1.txt
ROBOCOPY "\\10.0.5.100\PrivateData2\." "D:\PrivateData2\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayPrivate2.txt
ROBOCOPY "\\10.0.5.100\PrivateData3\." "D:\PrivateData3\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayPrivate3.txt
ROBOCOPY "\\10.0.5.100\HomeworkArchive\." "D:\HomeworkArchive\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayHomeworkArchive.txt
ROBOCOPY "\\172.20.255.125\PortableDeviceFilesArchive\." "D:\PortableDeviceFilesArchive\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayPorDevArch.txt
ROBOCOPY "\\172.20.255.3\wamp\." "D:\3wampBackups\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayWAMPSERVER.txt
ROBOCOPY "\\172.20.255.123\wamp\." "D:\123wampBackups\." /COPYALL /B /SEC /MIR /R:0 /W:0 /LOG:C:\backups\LOGS\Monday\MondayWAMPSERVER123.txt

# Wait for files to finish
sleep 60

# Zip logs
& $path a Monday.zip Monday*.txt

# Copy to network
cp Monday.zip  "$date.zip"
cp "$date.zip" "\\10.0.5.100\PrivateData\Resource\Network\backuplogs\Monday\$date.zip"

# Wait for network traffic to stop
sleep 120

# Close network
netsh interface set interface ethernet disabled

2 个答案:

答案 0 :(得分:0)

正如您在自己的评论中所提到的(顺便提一下,请edit您的问题,而不是将来对其进行评论),这可以通过使用Internet Explorer ComObject来完成:

$IE=new-object -com internetexplorer.application 
$IE.navigate2("www.microsoft.com") 
$IE.visible=$true

# do more stuff until done with IE

$IE.Close()

通过这种方式,您可以更好地控制IE,并且可以执行诸如检查页面是否仍在加载的情况,我想您在运行脚本时可能会这样做,或者如果不是,您的脚本可能在完成后转发到新页面,或者进行一些您的comobject可以检测到的更改,以便您知道何时移动或关闭该站点。

答案 1 :(得分:0)

你还没有描述当脚本遇到第21行时的问题。我将假设,在没有运行你的脚本的情况下,前一行成功检索了IE中的网页但是所需的效果是这个窗口立即关闭,而taskkill命令无法关闭IE。

如果您想要的是在不离开打开的浏览器窗口的情况下点击Web地址,我建议更简单的方法是直接从PowerShell请求Web地址并完全跳过浏览器。

在PowerShell v3中实现此目的的一种方法是使用Invoke-WebRequest,如下所示:

Invoke-WebRequest -Uri "http://172.20.255.3/NeikoDev/Backup%20emails/Backup%20Emails.php"

但是这里列出了类似的替代方案:http requests with powershell