标签: powershell

如何找到执行脚本的源路径?

我希望能够告诉我执行脚本的运行路径.
这往往不是$ pwd.

我需要调用相对于我的脚本的文件夹结构中的其他脚本,虽然我可以对路径进行硬编码,但是当尝试从"开发"升级到"测试"时,这既令人反感又有点痛苦"生产".

powershell powershell-1.0

61
推荐指数
5
解决办法
8万
查看次数

桌面路径的环境变量是什么?

我正在编写Windows批处理文件,并希望将某些内容复制到桌面.我想我可以用这个:

%USERPROFILE%\桌面\

但是,我在想,这可能只适用于英语操作系统.有没有办法可以在批处理文件中执行此操作,该文件适用于任何国际化版本?

UPDATE

我尝试了以下批处理文件:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Run Code Online (Sandbox Code Playgroud)

得到了这个输出:

S:\>REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
    Desktop    REG_EXPAND_SZ    %USERPROFILE%\Desktop


S:\>FOR /F "usebackq tokens=3 skip=4" %i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folder
s" /v Desktop`) DO SET DESKTOPDIR=%i

S:\>FOR /F "usebackq …

windows vbscript powershell desktop localization

61
推荐指数
5
解决办法
20万
查看次数

如何获取PowerShell脚本的文件系统位置?

我有一个PowerShell脚本D:\temp.

当我运行此脚本时,我希望列出该文件的当前位置.我该怎么做呢?

例如,此代码将在DOS批处理文件中完成; 我想将其转换为PowerShell脚本...

FOR /f "usebackq tokens=*" %%a IN ('%0') DO SET this_cmds_dir=%%~dpa
CD /d "%this_cmds_dir%"
Run Code Online (Sandbox Code Playgroud)

powershell batch-file

61
推荐指数
2
解决办法
8万
查看次数

Windows 7上的PowerShell:常规用户的Set-ExecutionPolicy

我想以普通用户身份在Windows 7上运行PowerShell脚本.每当我尝试时,我都会收到以下错误:

File C:\Users\danv\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because the
execution of scripts is disabled on this system. Please see "get-help about_signing" for
more details.
At line:1 char:2
+ . <<<<  'C:\Users\danv\Documents\WindowsPowerShell\profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)

尝试解决via Set-ExecutionPolicy Unrestricted失败:

PS C:\Users\danv> Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'
is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Run Code Online (Sandbox Code Playgroud)

我可以Set-ExecutionPolicy …

powershell windows-7

61
推荐指数
3
解决办法
11万
查看次数

在Powershell中创建日志文件

我有以下代码,目前它加载屏幕上的所有信息.我希望它登录到D:\ Apps\Logs上的日志文件.

日志文件需要具有要加载的计算机的名称 - 所以COMPUTERNAME.log

知道我怎么能这样做吗?

谢谢

$computer = gc env:computername

$onetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMajorPart).tostring() $twotcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMinorPart).tostring() $threetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductBuildPart).tostring() $fourtcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductPrivatePart).tostring()


$onedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMajorPart).tostring() $twodfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMinorPart).tostring() $threedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductBuildPart).tostring() $fourdfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductPrivatePart).tostring()

write-host TCPIP.sys Version on $computer is: "$onetcp.$twotcp.$threetcp.$fourtcp" Write-Host write-host DFSRS.exe Version on $computer is: "$onedfsr.$twodfsr.$threedfsr.$fourdfsr"

Write-Host

If (get-wmiobject win32_share | where-object {$_.Name -eq "REMINST"}) {   Write-Host "The REMINST share exists on $computer" } Else {   Write-Host "The …
Run Code Online (Sandbox Code Playgroud)

powershell

61
推荐指数
6
解决办法
29万
查看次数

获取Powershell循环中当前项的索引?

给定PowerShell中的项目列表,如何在循环中找到当前项目的索引?

例如:

$letters = { 'A', 'B', 'C' }

$letters | % {
  # Can I easily get the index of $_ here?
}
Run Code Online (Sandbox Code Playgroud)

所有这一切的目标是我想使用Format-Table输出一个集合,并添加一个包含当前项目索引的初始列.通过这种方式,人们可以交互式地选择要选择的项目.

powershell

60
推荐指数
4
解决办法
10万
查看次数

Copy-Item将目录以及内容复制到UNC路径

我正在尝试使用PowerShell 1.0获取文件夹的内容并将其复制到另一个文件夹.非常简单的东西,Copy-Item $from $to -recurse如果我从本地文件夹复制到本地文件夹,它一切正常.但是,如果$to变量是UNC路径,它似乎复制$from目录,而不仅仅是其内容.

例如

$from = "c:\temp\rhysc\" 
$to = "\\OtherMachineName\ShareFolder\"  
Copy-Item $from $to -recurse
Run Code Online (Sandbox Code Playgroud)

...最终创建一个文件夹,\\OtherMachineName\ShareFolder\rhysc而不是仅仅复制文件夹的内容.

我想维护$from我复制的目录的结构,所以我对管道的基本尝试不起作用(所有内容都被转储到$to文件夹的根目录中)

Get-ChildItem $from -recurse | Copy-Item -destination $to
Run Code Online (Sandbox Code Playgroud)

powershell unc powershell-1.0

60
推荐指数
1
解决办法
8万
查看次数

Powershell 2 copy-item如果不存在则创建一个文件夹

$from = "\\something\1 XLS\2010_04_22\*"
$to =  "c:\out\1 XLS\2010_04_22\"
copy-item $from $to -Recurse 
Run Code Online (Sandbox Code Playgroud)

如果c:\ out\1 XLS\2010_04_22 \确实存在,则此方法有效.如果不存在,是否可以使用单个命令创建"c:\ out\1 XLS\2010_04_22 \".

powershell copy powershell-2.0

60
推荐指数
3
解决办法
11万
查看次数

如何为Powershell脚本参数显示帮助消息?

我有一个powershell脚本(setup.ps1),我们将其用作开发环境设置脚本的入口点.它需要一个参数:

param(
    [Parameter(Position=0,HelpMessage="The targets to run.")]
    [Alias("t")]
    [string[]]
    $Targets = "Help"
)
Run Code Online (Sandbox Code Playgroud)

我跑的时候

PS > get-help .\setup.ps1 -detailed
Run Code Online (Sandbox Code Playgroud)

在参数部分,我的帮助消息没有出现:

PARAMETERS
    -Targets <String[]>
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能显示参数帮助消息?

powershell powershell-2.0

60
推荐指数
3
解决办法
7万
查看次数

如何从模块中检索可用命令?

要了解计算机上可用的PowerShell模块,请使用该命令

Get-Module -ListAvailable
Run Code Online (Sandbox Code Playgroud)

这将返回包含module-type,-name和导出命令的列表.但导出的命令始终为空并且只显示{}.为什么不显示?

我是否必须使用其他参数或是否有另一个cmdlet或方法来检索可用的命令?

powershell module powershell-2.0

59
推荐指数
2
解决办法
8万
查看次数