39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_run_ip_port_l2w_user_pwd.ps1 6.00 KB
一键复制 编辑 原始数据 按行查看 历史
#建议保存编码为:bom头 + utf8
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$目的ip地址,
[Alias("port")][uint16]$端口 = 5985,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("user")][String]$用户名,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("pwd")][String]$密码,
[Alias('file')][String]$ps1脚本文件名,
[Alias("allparameter")]$传入参数,
[parameter(Position = 0)][Alias("scriptblock")][scriptblock]$powershell代码块 = { },
$复制主控机node_script目录到被控机 = $false,
[ValidateSet('microsoft.powershell','powershell.7')]
[string]$ConfigurationName = 'microsoft.powershell'
)
if ($用户名.contains('\') )
{
Write-Error '错误:用户名不应该有"\"。退出码31'
exit 31
}
function yg严格测试布尔型变量($被测试的布尔形变量)
{
if (($被测试的布尔形变量 -eq 1) -or ($被测试的布尔形变量 -eq $true) -or ($被测试的布尔形变量 -eq 'true'))
{
return $true
}
elseif (($被测试的布尔形变量 -eq 0) -or ($被测试的布尔形变量 -eq $false) -or ($被测试的布尔形变量 -eq 'false'))
{
return $false
}
else
{
Write-Error '错误:不合法的布尔型值,错误码999'
exit 999
}
}
[bool]$复制主控机node_script目录到被控机 = yg严格测试布尔型变量 $复制主控机node_script目录到被控机
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'
}
& "${global:kasini3000目录}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
Write-Error "应该使用另一个脚本!"
exit 21
}
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.'
}
if ( ($ps1脚本文件名 -eq $null) -or ($ps1脚本文件名 -eq '') )
{
#这段代码在run_win2linux_key_pwd.ps1,run_win2win5985_pwd.ps1,run_linux2linux_key_pwd.ps1,k-commit.ps1
$有脚本文件 = $false
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
Write-Error "错误:没有输入脚本文件名,同时也没有输入代码块"
exit 15
}
else
{
if (& 'kcd_被控机运行的代码块_含有kcd.ps1' -输入脚本代码块 $powershell代码块)
{
Write-Error '错误:脚本内含有kcd等关键字'
exit 19
}
}
}
else
{
$有脚本文件 = $true
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
}
else
{
Write-Error "错误:有输入脚本文件名,同时有输入代码块"
exit 16
}
if (Test-Path -LiteralPath $ps1脚本文件名)
{
if (& 'kcd_被控机运行的脚本_含有kcd.ps1' -输入脚本文件名 $ps1脚本文件名)
{
Write-Error '错误!脚本内含有kcd等关键字'
exit 18
}
}
else
{
Write-Error "找不到脚本文件"
exit 17
}
}
if ($IsLinux -eq $True)
{
# if ($PSVersionTable.psversion.major -gt 6)
# {
# if ($env:LANG -eq 'zh_CN.UTF-8')
# {
# Write-Warning "警告:powershell v7 版本,及以上,遇到centos8,会有问题。"
# }
# else
# {
# Write-Error "Warning: powershell v7 and above, encounter centos8, there will be problems."
# }
# }
if ($复制主控机node_script目录到被控机 -eq $true)
{
& 'cpn21复制主控机node_script目录到当前被控机.ps1'
}
$端口通了 = & '/etc/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
}
Write-Verbose "linux2win,开始在被控机【${目的ip地址}】上执行"
$用户名2 = "${目的ip地址}\${用户名}"
$密码密文 = ConvertTo-SecureString $密码 -AsPlainText -Force
$用户名和密码捆绑后的授权信息 = New-Object System.Management.Automation.PSCredential ($用户名2,$密码密文)
Write-Verbose '使用密码,连接linux2win5985开始'
$private:连接l2w01 = New-PSSession -ComputerName $目的ip地址 -Port $端口 -Credential $用户名和密码捆绑后的授权信息 -Authentication Negotiate -ConfigurationName $ConfigurationName
if ( ($private:连接l2w01 -eq $null) -or ($private:连接l2w01 -eq '') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码连接失败。错误码 25"
}
else
{
Write-Error "error:node port cloesd.exit 25"
}
exit 25
}
Write-Verbose '连接成功。现在开始执行命令:'
[scriptblock]$private:cmd1 = `
{
Test-Path -LiteralPath 'c:\ProgramData\kasini3000'
}
$private:返回 = Invoke-Command -Session $private:连接l2w01 -ScriptBlock $private:cmd1
if ($private:返回 -eq $true)
{
}
else
{
[scriptblock]$private:cmd2 = `
{
mkdir 'c:\ProgramData\kasini3000'
}
Invoke-Command -Session $private:连接l2w01 -ScriptBlock $private:cmd2
$env:LANG = 'zh_CN.UTF-8'
Copy-Item -LiteralPath '/etc/kasini3000/node_script' -Destination 'c:\ProgramData\kasini3000\' -Recurse -ToSession $private:连接l2w01
}
Invoke-Command -Session $private:连接l2w01 -ScriptBlock { & 'c:\ProgramData\kasini3000\node_script\kasini3000\add_path.ps1' }
if ( $有脚本文件 -eq $true)
{
Invoke-Command -Session $private:连接l2w01 -FilePath $ps1脚本文件名 -ArgumentList $传入参数
}
else
{
Invoke-Command -Session $private:连接l2w01 -ScriptBlock $powershell代码块 -ArgumentList $传入参数
}
Write-Verbose '执行命令完成,即将断开连接。'
Remove-PSSession -Session $private:连接l2w01
}
Write-Verbose "linux2win,完成在被控机【${目的ip地址}】上执行"
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助