1 Star 0 Fork 54

Yuna/kasini3000

forked from chuanjiao10/kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyto_ip_port_w2l_user.ps1 5.78 KB
一键复制 编辑 原始数据 按行查看 历史
#建议保存编码为:bom头 + utf8
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$目的ip地址,
[Alias("port")][uint16]$端口 = 22,
[ValidateNotNullOrEmpty()]
[Alias("user")][String]$用户名 = 'root',
[String]$ssh_key_file_path = '',
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$LiteralPath,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Destination,
[Switch]$Recurse,
#缩水封装
[String[]]$Exclude #源目录,需要使用【-path】和【*】才灵。如: -path 'd:\xxx\*'
)
if (Test-Path -LiteralPath $LiteralPath)
{
}
else
{
Write-Error "错误:找不到源文件,或源目录。退出码4"
exit 4
}
if ($IsLinux -eq $True)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Warning '错误:应该使用另一个脚本。错误码22'
}
else
{
Write-Warning 'Error: Another script should be used.exit 22'
}
exit 22
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
& 'c:\ProgramData\kasini3000\0k_source.ps1'
}
Write-Verbose '从主控机到被控机,复制文件开始'
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Warning '本脚本限制:需要用户交互输入密码,无法自动。'
}
else
{
Write-Warning 'Limitation of this script: It requires the user to enter the password interactively, not automatically.'
}
$端口通了 = & 'c:\ProgramData\kasini3000\tcp--ping-v5.ps1' -MyComputerName $目的ip地址 -port $端口 -Quiet
if ($端口通了 -eq $true)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:被控机端口不通。错误码24"
}
else
{
Write-Error "error:node port cloesd.exit 24"
}
exit 24
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ( $PSVersionTable.psversion.major -ge 6)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:在win中连接linux,依赖powershell 7.2,或以上版本。退出码12"
}
else
{
Write-Error "error: From windows connecting to linux , need powershell version 7.2, or above. Exit code 12"
}
exit 12
}
[string]$temp001 = ssh.exe -V *>&1
if (-not ($temp001.ToLower().Contains('ssh'.ToLower())) )
{
Write-Error '管理linux被控机,需要ssh.exe。请运行【zkj_install_k记饭店_win主控机.ps1】'
exit 23
}
if ($ssh_key_file_path -eq '')
{
Write-Verbose '使用ssh密码,连接开始'
$private:连接w2l21 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名
if ( ($private:连接w2l21 -eq $null) -or ($private:连接w2l21 -eq '') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码连接失败。错误码25"
}
else
{
Write-Error "error:node port cloesd.exit 25"
}
exit 25
}
}
else
{
Write-Verbose '使用ssh秘钥,连接开始'
$private:连接w2l21 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名 -KeyFilePath $ssh_key_file_path
if ( ($private:连接w2l21 -eq $null) -or ($private:连接w2l21 -eq '') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码连接失败。错误码26"
}
else
{
Write-Error "error:node port cloesd.exit 26"
}
exit 26
}
}
Write-Verbose '连接成功。现在开始执行命令:'
$主控机公钥sha1 = Get-FileHash -Algorithm sha1 -LiteralPath "${global:kasini3000_data_path}\ssh_key_files_old1\authorized_keys"
$被控机公钥sha1 = Invoke-Command -Session $private:连接w2l21 -ScriptBlock { Get-FileHash -Algorithm sha1 -LiteralPath '/root/.ssh/authorized_keys' }
Write-Verbose ("公钥: {0} '---' {1} " -f ${主控机公钥sha1}.Hash,${被控机公钥sha1}.Hash)
if ($主控机公钥sha1.Hash -ne $被控机公钥sha1.Hash)
{
Write-Verbose "主控机-被控机之间,公钥有不同"
Copy-Item -LiteralPath "${global:kasini3000_data_path}\ssh_key_files_old1\authorized_keys" -Destination '/root/.ssh/' -ToSession $private:连接w2l21 -Force
}
[scriptblock]$private:cmd1 = `
{
Test-Path -LiteralPath '/etc/kasini3000'
}
$private:返回 = Invoke-Command -Session $private:连接w2l21 -ScriptBlock $private:cmd1
if ($private:返回 -eq $true)
{
}
else
{
[scriptblock]$private:cmd2 = `
{
mkdir '/etc/kasini3000'
}
Invoke-Command -Session $private:连接w2l21 -ScriptBlock $private:cmd2
$env:LANG = 'zh_CN.UTF-8'
Copy-Item -LiteralPath 'c:\ProgramData\kasini3000\node_script' -Destination '/etc/kasini3000' -Recurse -ToSession $private:连接w2l21
}
Invoke-Command -Session $private:连接w2l21 -ScriptBlock {
if (Test-Path '/etc/kasini3000/node_script/kasini3000/add_path.ps1')
{
& '/etc/kasini3000/node_script/kasini3000/add_path.ps1'
}
}
Write-Verbose '连接成功。现在开始测试Destination是否存在:'
[scriptblock]$private:cmd3 = `
{
Test-Path -LiteralPath $args[0]
}
$private:返回 = Invoke-Command -Session $private:连接w2l21 -ScriptBlock $private:cmd3 -ArgumentList $Destination
if ($private:返回 -eq $true)
{
Write-Verbose '连接成功。现在开始复制文件:'
if ($Recurse -eq $True)
{
Copy-Item -ToSession $private:连接w2l21 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
Copy-Item -ToSession $private:连接w2l21 -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude
}
Write-Verbose '复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接w2l21
}
else
{
Remove-PSSession -Session $private:连接w2l21
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到目标目录。错误码5"
}
else
{
Write-Error "error:The target directory could not be found.exit 5"
}
exit 5
}
}
Write-Verbose '从主控机到被控机,复制文件完成'
exit 0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PowerShell
1
https://gitee.com/YunaCode/kasini3000.git
git@gitee.com:YunaCode/kasini3000.git
YunaCode
kasini3000
kasini3000
master

搜索帮助