Powershell管理系列(三十五)PowerShell操作之以管理员权限运行脚本

简介:

场景:Win10或者是非管理员权限运行命令行的时候,提示无权限,如下图

wKioL1gI5GaBQ12sAAAf6nXdprg670.png-wh_50

我们可以右键Powershell,以管理员权限运行,或者是计划任务设置为以管理员权限运行,其实我们有更加高效的办法,可以直接提权运行脚本,如下图,输入用户名和密码,

Invoke-Command -ScriptBlock {ipconfig /registerdns} -ComputerName desktop-jq6usg3 -Credential (Get-Credential)

wKiom1gI52rQM4pEAAEYFwYX8l8840.png-wh_50

结果运行如下,命令成功执行,没有提示权限不足。

wKioL1gI52uCfAYbAABxmOfQHLI685.png-wh_50

如果不需要输入用户名和密码,并且把命令存入到 .ps1脚本,如下:

$UserName = "administrator"      #定义管理员账户名称
$Password = ConvertTo-SecureString Aa12345678 -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
Invoke-Command -Credential $cred -command {C:\Users\Terry.zhou\Desktop\host.ps1 } -ComputerName DESKTOP-JQ6USG3

wKioL1gI6NTSEVyrAADiqMbnIgE203.png-wh_50

wKiom1gI6S2TYDMzAAAnDQPBXQk130.png-wh_50




本文转自 zhou_ping 51CTO博客,原文链接:http://blog.51cto.com/yuntcloud/1864075,如需转载请自行联系原作者


相关文章
|
4月前
|
存储 安全 Windows
PowerShell系列(六):PowerShell脚本执行策略梳理
【2月更文挑战第1篇】PowerShell 脚本执行策略用于控制何时以及何种方式执行 PowerShell 脚
|
1月前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
1月前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
1月前
|
存储 Shell 容器
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
|
1月前
【Azure 应用服务】Azure Function 中运行Powershell 脚本,定位 -DefaultProfile 引发的错误
【Azure 应用服务】Azure Function 中运行Powershell 脚本,定位 -DefaultProfile 引发的错误
|
1月前
|
Java
【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  
【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  
|
1月前
|
Java 开发工具 Windows
【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
|
1月前
|
缓存 监控 NoSQL
【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
|
1月前
|
存储 数据库
【Azure 应用服务】在Azure Funciton中使用Powershell脚本函数,需要存储一些变量值如何解决?
【Azure 应用服务】在Azure Funciton中使用Powershell脚本函数,需要存储一些变量值如何解决?
|
3月前
|
存储 SQL 运维
使用PowerShell进行自动化脚本编写:入门与实战
【6月更文挑战第6天】本文介绍了PowerShell作为Windows系统管理的自动化工具,用于提升效率和减少错误。内容涵盖PowerShell基础,如变量、命令执行、管道、条件和循环语句、函数。实战案例展示了如何用PowerShell脚本进行文件备份。此外,还提及PowerShell的进阶功能,如模块、远程管理和与其他工具集成。学习和应用PowerShell能有效提升IT运维自动化水平。