当前位置: 首页> 健康> 科研 > 工商信息查询网官网_广告联盟app手机版_企业网站搜索引擎推广方法_sem是什么基团

工商信息查询网官网_广告联盟app手机版_企业网站搜索引擎推广方法_sem是什么基团

时间:2025/7/12 16:18:49来源:https://blog.csdn.net/hknaruto/article/details/143055211 浏览次数:0次
工商信息查询网官网_广告联盟app手机版_企业网站搜索引擎推广方法_sem是什么基团

脚本

function Parse-IniFile {param([string]$Path)$ini = @{}switch -regex -file $Path {"^\[(.+)\]$" {$section = $matches[1]$ini[$section] = @{}}"(.+?)=(.+)" {$name, $value = $matches[1..2]$ini[$section][$name] = $value}}return $ini
}function GetCredential {param ([string]$Path,[string]$serverIp)# 读取ini$ini = Parse-IniFile -Path $Path$user = $ini[$serverIp]["User"]$password = $ini[$serverIp]["Password"]	#Write-Host $user#Write-Host $password# 创建 PSCredential 对象$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $securePasswordreturn $credential
}# 将本地文件放到远程服务器上,自动创建对应目录
function Put {param ([string]$localFile,[Object]$credential,[string]$serverIp,        [string]$remoteFile)# 检查本地文件是否存在if (-not (Test-Path -Path $localFile)) {Write-Error "Local file does not exist: $localFile"return}# 提取远程文件的目录路径$remoteDir = Split-Path -Path $remoteFile -Parent# 创建远程会话$SessionOption = New-PSSessionOption -SkipCNCheck -SkipCACheck$session = New-PSSession -ComputerName $serverIp -Credential $credential -Port 5986 -UseSSL  -SessionOption $SessionOption -ErrorAction Stoptry {# 在远程服务器上创建目录(如果不存在)Invoke-Command -Session $session -ScriptBlock {param ($dir)if (-not (Test-Path -Path $dir)) {New-Item -ItemType Directory -Path $dir -Force}} -ArgumentList $remoteDir# 复制文件到远程服务器Copy-Item -Path $localFile -Destination $remoteFile -ToSession $session -ForceWrite-Output "File copied successfully to $remoteFile on $serverIp"} catch {Write-Error "Failed to copy file: $_"} finally {# 关闭远程会话Remove-PSSession -Session $session}
}# 在远程执行PowerShell指令
function Run {param ([Object]$credential,[string]$serverIp,[string]$pscmd)# 创建远程会话$SessionOption = New-PSSessionOption -SkipCNCheck -SkipCACheck$session = New-PSSession -ComputerName $serverIp -Credential $credential -Port 5986 -UseSSL  -SessionOption $SessionOption -ErrorAction Stoptry {# 在远程服务器上执行命令$result = Invoke-Command -Session $session -ScriptBlock {param ($cmd)Invoke-Expression $cmd} -ArgumentList $pscmd# 输出结果Write-Output $result} catch {Write-Error "Failed to run command: $_"} finally {# 关闭远程会话Remove-PSSession -Session $session}
}

ini文件结构

[server1]
User=user1
Password=password1[Server2]
User=user2
Password=password2[Server3]
User=user3
Password=password3

调用示例

调用效果

关键字:工商信息查询网官网_广告联盟app手机版_企业网站搜索引擎推广方法_sem是什么基团

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: