Chinaunix

标题: 关于powershell上传文件到远程服务器 [打印本页]

作者: 夏寥寥    时间: 2016-06-23 15:29
标题: 关于powershell上传文件到远程服务器
在网上搜索到一个使用powershell从本地上传文件到远程服务器的脚本,但其中一个地方不太明白,还请各位大神帮忙解答下!谢谢!
不太明白这句 <RemoteDirPath>\\远程主机IP地址\D$\test</RemoteDirPath>的D$是什么意思?是指远程主机的D盘吗?


脚本名:AutoCopyfile.ps1
  1. [string]$xmldocpath = "E:\DOE\Test\Config.xml"                  #读取配置文件Config.xml

  2. $xmlDoc = New-Object "system.xml.xmldocument"
  3. $xmlDoc.Load($xmldocpath)
  4. $nodeList=$xmlDoc.GetElementsByTagName("Server");

  5. foreach($node in $nodeList){
  6.         $childNodes = $node.ChildNodes
  7.        
  8.         $localDirPath = $childNodes.Item(0).InnerXml.ToString()         # 本地待拷贝文件目录
  9.         $remoteDirPath = $childNodes.Item(1).InnerXml.ToString()        # 远程文件目录
  10.         $remoteLoginName = $childNodes.Item(2).InnerXml.ToString()      # 远程登录用户名
  11.         $remotePwd = $childNodes.Item(3).InnerXml.ToString()            # 远程登录密码
  12.         $include = $childNodes.Item(4).InnerXml.ToString().Split(',')   # 要包含的文件格式
  13.         $exclude = $childNodes.Item(5).InnerXml.ToString().Split(',')          # 要排除的文件格式
  14.        
  15.         Write-Host '将从' $localDirPath '本地目录拷贝文件到 '$remoteDirPath' 远程目录中'
  16.         Write-Host '  待包含的文件格式:'$include,'待排除的文件格式:'$exclude
  17.        
  18.         Write-Host '连接远程主机...'
  19.         net use $remoteDirPath $remotePwd /user:$remoteLoginName
  20.        
  21.         Write-Host '递归拷贝文件(强制覆盖模式)...'
  22.        
  23.         $files = Get-ChildItem -Path $localDirPath # 获取本地目录下的文件
  24.         foreach($file in $files) {
  25.                 Copy-Item -Path $file.FullName -Destination $remoteDirPath -Include $include -Exclude $exclude -Recurse -Force
  26.         }

  27.         Write-Host '目录拷贝完成!'
  28. }
复制代码
配置文件:Config.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Servers>
  3.    <Server>
  4.       <LocalDirPath>D:\test</LocalDirPath>
  5.       <RemoteDirPath>\\远程主机IP地址\D$\test</RemoteDirPath>
  6.       <RemoteLoginName>Domain名\你的远程登录用户名</RemoteLoginName>
  7.       <RemotePwd>你的远程登录密码</RemotePwd>
  8.       <Include>*.dll,*.xml</Include>
  9.       <Exclude>*.txt</Exclude>
  10.    </Server>
  11. </Servers>
复制代码

作者: 夏寥寥    时间: 2016-06-23 16:00
是WINDOWS下的默认共享,D$就是共享D盘,打开cmd,输入net share就能查看哪些盘是共享的!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2