1 Star 1 Fork 54

dotnet爱好者/kasini3000

forked from chuanjiao10/kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyfrom_ip.ps1 19.23 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
#建议保存编码为:bom头 + utf8
param
(
[Alias("ipaddress")][String]${被控机ip地址} = $global:当前被控机_ip.ip,
[Alias("port")][uint16]$端口 = $global:当前被控机_ip.端口,
[String]$LiteralPath,
[String]$Path,
[ValidateNotNullOrEmpty()][String]$Destination,
[Switch]$Recurse,
#缩水封装
[String[]]$Exclude #源目录,需要使用【-path】和【*】才灵。如: -path 'd:\xxx\*'
)
if (($LiteralPath -eq '') -and ($Path -eq ''))
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:源文件,源目录为空。退出码6"
}
else
{
Write-Error "error:Source file, or source directory is empty .exit 6"
}
exit 6
}
if (($LiteralPath -ne '') -and ($Path -ne ''))
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:-LiteralPath,-Path,只能输入其一。退出码7"
}
else
{
Write-Error "error:-LiteralPath,-Path,Only one of them can be entered .exit 7"
}
exit 7
}
if (Test-Path -LiteralPath $Destination)
{
}
else
{
Write-Error "错误:找不到目标目录。退出码4"
exit 4
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
& 'c:\ProgramData\kasini3000\0k_source.ps1'
}
if ($IsLinux -eq $True)
{
& '/etc/kasini3000/0k_source.ps1'
}
Write-Verbose '从被控机到主控机,复制文件开始'
& 'zd只读nodelist文件.ps1'
$private:当前被控机 = $global:所有被控机 | Where-Object { $_.ip -eq $被控机ip地址 }
if ($private:当前被控机.ip -ne $被控机ip地址)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "nodelist文件中找不到这个ip地址: ${被控机ip地址} ,错误码11"
}
else
{
Write-Error "NODE IP was not found in the NODELIST file: ${被控机ip地址} ,error code 11"
}
$private:当前被控机.'被控机os类型' = '错误:nodelist文件中找不到这个ip地址'
exit 11
}
$win = 'win7','win8','win10','win11','win2008r2','win2012r2','win2016','win2019','win2022'
$linux = & "${PSScriptRoot}/linux_distribution_str.ps1"
$all = $win + $linux
Write-Verbose $private:当前被控机.被控机os类型
Write-Verbose $private:当前被控机.ip
if ($private:当前被控机.被控机os类型 -notin $all)
{
& 'sx1刷新单个被控机对象的_os类型属性.ps1' -被控机ip地址 $private:当前被控机.ip
}
Write-Verbose $private:当前被控机.被控机os类型
if ($private:当前被控机.被控机os类型 -notin $all)
{
Write-Error ("错误:当前被控机【{0}】,【端口不通】或【账户密码不对】。任务无法运行。退出码24" -f $private:当前被控机.ip)
exit 24
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ( $private:当前被控机.被控机os类型 -in $win)
{
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 5985
}
try
{
$null = Test-WSMan -ComputerName ${被控机ip地址} -Port $端口 -ErrorAction stop
}
catch
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:目的ip地址【${被控机ip地址}】端口不通。错误码22"
}
else
{
Write-Error "error:node port closed :【${被控机ip地址}】.exit 22"
}
exit 22
}
finally
{
}
$PSRemoting服务器用户名 = $private:当前被控机.用户名
$用户名2 = "${被控机ip地址}\${PSRemoting服务器用户名}"
$密码明文 = $private:当前被控机.当前密码
$密码密文 = ConvertTo-SecureString $密码明文 -AsPlainText -Force
$用户名和密码捆绑后的授权信息 = New-Object System.Management.Automation.PSCredential ($用户名2,$密码密文)
Write-Verbose '使用密码,连接w2w5985开始'
$private:连接3 = New-PSSession -ComputerName ${被控机ip地址} -Port $端口 -Credential $用户名和密码捆绑后的授权信息
if ($private:连接3 -eq $null)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码,在${被控机ip地址}上连接w2w5985失败。退出码21"
}
else
{
Write-Error "error:Connection failed: ${被控机ip地址} .exit 21"
}
exit 21
}
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接3 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接3 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接3
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Exclude -eq $null)
{
try
{
if ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
else
{
try
{
if ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
}
else
{
try
{
if ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接3 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
Copy-Item -FromSession $private:连接3 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
Write-Verbose '复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接3
}
if ( $private:当前被控机.被控机os类型 -in $linux)
{
if ( $PSVersionTable.psversion.major -ge 6)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:在win中连接linux,依赖powershell 6,或以上版本。错误码12"
}
else
{
Write-Error "error:need powershell version 6 or higher.exit 12"
}
exit 12
}
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 22
}
Write-Verbose '使用ssh秘钥1,连接w2l开始'
[string]$private:temp011 = ssh.exe -l root -i "$env:USERPROFILE\.ssh\id_rsa" ${被控机ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp011
if ( $private:temp011.ToLower().Contains('Permission denied'.ToLower()) )
{
$秘钥1连接成功 = $false
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Verbose "错误:使用ssh秘钥1,在${被控机ip地址}上连接w2l失败"
}
else
{
Write-Verbose "error:ssh-key-file1 login failed:${被控机ip地址}"
}
}
else
{
$秘钥1连接成功 = $true
}
if ( $private:temp011.ToLower().Contains('timed out'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥1,在${被控机ip地址}上w2l连接超时。退出码13"
}
else
{
Write-Error "error:ssh-key-file1 login timeout: ${被控机ip地址} .exit 13"
}
exit 13
}
if ($秘钥1连接成功 -eq $true)
{
$private:连接1 = New-PSSession -HostName ${被控机ip地址} -Port $端口 -UserName root -KeyFilePath "$env:USERPROFILE\.ssh\id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接1 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接1 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接1
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接1】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -Path $Path -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -Path $Path -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
Write-Verbose '【连接1】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接1
}
else
{
Write-Verbose '使用ssh秘钥2,连接w2l开始'
[string]$private:temp012 = ssh.exe -l root -i "${global:kasini3000_data_path}\ssh_key_files_old1\id_rsa" ${被控机ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp012
if ( $private:temp012.ToLower().Contains('Permission denied'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥2,在${被控机ip地址}上连接w2l失败。错误码15"
}
else
{
Write-Error "error:ssh-key-file2 login failed${被控机ip地址}.exit 15"
}
exit 15
}
$private:连接2 = New-PSSession -HostName ${被控机ip地址} -Port $端口 -UserName root -KeyFilePath "${global:kasini3000_data_path}\ssh_key_files_old1\id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接2 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接2 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接2
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接2】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -Path $Path -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -Path $Path -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
Write-Verbose '【连接2】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接2
}
}
}
if ($IsLinux -eq $True)
{
if ( $private:当前被控机.被控机os类型 -in $win)
{
Write-Error "无法从【linux主控机】复制文件到【win被控机】"
exit 3
}
if ( $private:当前被控机.被控机os类型 -in $linux)
{
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 22
}
Write-Verbose '使用ssh秘钥1,连接l2l开始'
[string]$private:temp011 = ssh -l root -i '/root/.ssh/id_rsa' ${被控机ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp011
if ( $private:temp011.ToLower().Contains('Permission denied'.ToLower()) )
{
$秘钥1连接成功 = $false
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Verbose "错误:使用ssh秘钥1,在${被控机ip地址}上连接l2l失败"
}
else
{
Write-Verbose "error:ssh-key-file1 login failed:${被控机ip地址}"
}
}
else
{
$秘钥1连接成功 = $true
}
if ( $private:temp011.ToLower().Contains('timed out'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥1,在${被控机ip地址}上l2l连接超时。退出码13"
}
else
{
Write-Error "error:ssh-key-file1 login timeout: ${被控机ip地址} .exit 13"
}
exit 13
}
if ($秘钥1连接成功 -eq $true)
{
$private:连接5 = New-PSSession -HostName ${被控机ip地址} -Port $端口 -UserName root -KeyFilePath '/root/.ssh/id_rsa'
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接5 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接5 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接5
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接5】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Exclude -eq $null)
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
Write-Verbose '【连接5】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接5
}
else
{
Write-Verbose '使用ssh秘钥2,连接l2l开始'
[string]$private:temp012 = ssh -l root -i "${global:kasini3000_data_path}/ssh_key_files_old1/id_rsa" ${被控机ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp012
if ( $private:temp012.ToLower().Contains('Permission denied'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥2,在${被控机ip地址}上连接l2l失败。错误码15"
}
else
{
Write-Error "error:ssh-key-file2 login failed${被控机ip地址}.exit 15"
}
exit 15
}
$private:连接6 = New-PSSession -HostName ${被控机ip地址} -Port $端口 -UserName root -KeyFilePath "${global:kasini3000_data_path}/ssh_key_files_old1/id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接6 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接6 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接6
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接6】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Exclude -eq $null)
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force -Exclude $Exclude
}
}
catch
{
$private:复制出错 = $true
}
}
}
else
{
try
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
catch
{
$private:复制出错 = $true
}
}
Write-Verbose '【连接6】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接6
}
}
}
if ($private:复制出错 -eq $true)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:从此被控机 ${被控机ip地址} ,向主控机复制文件失败。错误码1"
}
else
{
Write-Error "error:failed copy from node: ${被控机ip地址} .exit 1"
}
exit 1
}
Write-Verbose '从被控机到主控机,复制文件完成'
exit 0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PowerShell
1
https://gitee.com/dotnetfans/kasini3000.git
[email protected]:dotnetfans/kasini3000.git
dotnetfans
kasini3000
kasini3000
master

搜索帮助