批量编程Runas实用程序创建器 [英] Batch programming a runas utility creater

查看:59
本文介绍了批量编程Runas实用程序创建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个批处理文件,使我更容易在计算机桌面上制作runas实用程序快捷方式.

I'm attempting to write a batch file that makes it easier for me to make runas utility shortcuts on a computers desktop.

我基本上已经完成了这一步,生成了所有变量,只是脚本的实际快捷方式创建部分遇到了问题.

I have this mostly done, I have all the variables generated, I'm just having issues with the actual shortcut creation part of the script.

这是我用来创建快捷方式的代码.通过此代码,我使用变量:%shortcutName%作为Internet Explorer%computername%是我的计算机名称,其中不包含任何特殊字符或空格,%user%是本地用户帐户,该帐户是管理员(再次没有特殊字符或空格),并且%UserInputpath%等于"C:\Program Files\Internet Explorer\iexplore.exe"(将文件拖放到命令提示符窗口时,它将生成此链接,并在需要时将其用引号引起来)

This is the code I am using to create the shortcut. With this code, I am using for variables: %shortcutName% as Internet Explorer, %computername% is my computers name, which doesn't include any special characters or spaces, %user% is a local user account which is an administrator (Again no special characters or spaces), and %UserInputpath% is equal to "C:\Program Files\Internet Explorer\iexplore.exe" (When you drag and drop a file into the command prompt window it generates this link, and wraps it in quotes if needed)

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%USERPROFILE%\Desktop\%shortcutName%.lnk');$s.TargetPath='runas /user:%computername%\%user% /savecred %UserInputPath%';$s.Save()"

我认为我的问题源于我之前所说的引号,但我不确定如何处理该问题.

I think that my problem stems from the quotes as I said earlier, but I'm not really sure how to handle the issue.

这是我得到的错误:

Value does not fall within the expected range.
At line:1 char:98
+ ... lorer.lnk');$s.TargetPath='runas /user:iamgroot\admin /savecred C ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

Unable to save shortcut "C:\Users\user\Desktop\Internet Explorer.lnk".
At line:1 char:203
+ ...  /savecred C:\Program Files\Internet Explorer\iexplore.exe';$s.Save()
+                                                                 ~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException

推荐答案

我的建议是,用此命令包装脚本...

My suggestion is you wrap your script with this...

#Run as Adminstrator

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments

%Your Coder HERE%

}

这小段代码解决了我所有的非管理员错误.

This little section of code resolved all my not administrator errors.

让我知道如何为您解决问题

Let me know how it works out for you,

UnderDog

这篇关于批量编程Runas实用程序创建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆