- 求職 : Linux運維
- 論壇徽章:
- 10
|
在網(wǎng)上搜索到一個使用powershell從本地上傳文件到遠程服務(wù)器的腳本,但其中一個地方不太明白,還請各位大神幫忙解答下!謝謝!
不太明白這句 <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 '目錄拷貝完成!'
- }
復(fù)制代碼 配置文件: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>
復(fù)制代碼 |
|