Chinaunix
标题:
关于powershell上传文件到远程服务器
[打印本页]
作者:
夏寥寥
时间:
2016-06-23 15:29
标题:
关于powershell上传文件到远程服务器
在网上搜索到一个使用powershell从本地上传文件到远程服务器的脚本,但其中一个地方不太明白,还请各位大神帮忙解答下!谢谢!
不太明白这句 <RemoteDirPath>\\远程主机IP地址\D$\test</RemoteDirPath>的D$是什么意思?是指远程主机的D盘吗?
脚本名:AutoCopyfile.ps1
[string]$xmldocpath = "E:\DOE\Test\Config.xml" #读取配置文件Config.xml
$xmlDoc = New-Object "system.xml.xmldocument"
$xmlDoc.Load($xmldocpath)
$nodeList=$xmlDoc.GetElementsByTagName("Server");
foreach($node in $nodeList){
$childNodes = $node.ChildNodes
$localDirPath = $childNodes.Item(0).InnerXml.ToString() # 本地待拷贝文件目录
$remoteDirPath = $childNodes.Item(1).InnerXml.ToString() # 远程文件目录
$remoteLoginName = $childNodes.Item(2).InnerXml.ToString() # 远程登录用户名
$remotePwd = $childNodes.Item(3).InnerXml.ToString() # 远程登录密码
$include = $childNodes.Item(4).InnerXml.ToString().Split(',') # 要包含的文件格式
$exclude = $childNodes.Item(5).InnerXml.ToString().Split(',') # 要排除的文件格式
Write-Host '将从' $localDirPath '本地目录拷贝文件到 '$remoteDirPath' 远程目录中'
Write-Host ' 待包含的文件格式:'$include,'待排除的文件格式:'$exclude
Write-Host '连接远程主机...'
net use $remoteDirPath $remotePwd /user:$remoteLoginName
Write-Host '递归拷贝文件(强制覆盖模式)...'
$files = Get-ChildItem -Path $localDirPath # 获取本地目录下的文件
foreach($file in $files) {
Copy-Item -Path $file.FullName -Destination $remoteDirPath -Include $include -Exclude $exclude -Recurse -Force
}
Write-Host '目录拷贝完成!'
}
复制代码
配置文件:Config.xml
<?xml version="1.0" encoding="utf-8"?>
<Servers>
<Server>
<LocalDirPath>D:\test</LocalDirPath>
<RemoteDirPath>\\远程主机IP地址\D$\test</RemoteDirPath>
<RemoteLoginName>Domain名\你的远程登录用户名</RemoteLoginName>
<RemotePwd>你的远程登录密码</RemotePwd>
<Include>*.dll,*.xml</Include>
<Exclude>*.txt</Exclude>
</Server>
</Servers>
复制代码
作者:
夏寥寥
时间:
2016-06-23 16:00
是WINDOWS下的默认共享,D$就是共享D盘,打开cmd,输入net share就能查看哪些盘是共享的!
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2