39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyfrom_ip_port_w2w_user_pwd.ps1 4.44 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]$密码,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$LiteralPath,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Destination,
[Switch]$Recurse,
#缩水封装
[String[]]$Exclude #源目录,需要使用【-path】和【*】才灵。如: -path 'd:\xxx\*'
)
if ($用户名.contains('\') )
{
Write-Error '错误:用户名不应该有"\"。退出码31'
exit 31
}
if (Test-Path -LiteralPath $Destination)
{
}
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
{
Write-Verbose '使用密码,连接w2w5985开始'
$PSRemoting服务器用户名 = $用户名
$用户名2 = "${目的ip地址}\${PSRemoting服务器用户名}"
$密码密文 = ConvertTo-SecureString $密码 -AsPlainText -Force
$用户名和密码捆绑后的授权信息 = New-Object System.Management.Automation.PSCredential ($用户名2,$密码密文)
$private:连接w2w11 = New-PSSession -ComputerName $目的ip地址 -Port $端口 -Credential $用户名和密码捆绑后的授权信息
if ($private:连接w2w11 -eq $null)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码,在${目的ip地址}上连接失败。错误码21"
}
else
{
Write-Error "error:connection failed.exit 21"
}
exit 21
}
[scriptblock]$private:cmd = `
{
if (Test-Path -LiteralPath 'c:\ProgramData\kasini3000\')
{
}
else
{
mkdir 'c:\ProgramData\kasini3000\'
}
return $PSVersionTable
}
$private:返回 = Invoke-Command -Session $private:连接w2w11 -ScriptBlock $private:cmd
Write-Verbose ($private:返回 | Out-String )
if ( ($private:返回.psversion.major -lt 6) -and ($private:返回.psversion.Minor -lt 1) ) #ps版本低于5.1
{
Write-Warning 'win被控机中的powershell版本太低'
}
Invoke-Command -Session $private:连接w2w11 -ScriptBlock {
if (Test-Path 'c:\ProgramData\kasini3000\node_script\kasini3000\add_path.ps1')
{
& 'c:\ProgramData\kasini3000\node_script\kasini3000\add_path.ps1'
}
}
Write-Verbose '连接成功。现在开始测试LiteralPath目录存在否:'
$Path存在否 = Invoke-Command -Session $private:连接w2w11 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接w2w11
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 ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接w2w11 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
Copy-Item -FromSession $private:连接w2w11 -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude
}
Write-Verbose '复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接w2w11
}
Write-Verbose '从主控机到被控机,复制文件完成'
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助