标签: powershell

如何使用powershell中的vim编辑UNC路径上的文件?

尝试使用 vim 编辑 UNC 路径上的文件时出现以下错误。我该如何解决?

> new-psdrive x filesystem \\192.168.1.1\c$
> cd x:\
> vim .\test.txt
'\\192.168.1.1\c$'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Run Code Online (Sandbox Code Playgroud)

windows powershell vim

5
推荐指数
1
解决办法
1632
查看次数

Powershell 中的 grep

使用 Powershell 内置函数来模拟grep类似行为的最简单/舒适的方法是什么?

在脚本中我使用这样的东西

dir "*.filter" | foreach-object{
    $actfile = $_
    $readerrorfile = [System.IO.Path]::GetTempFileName()
    $found = $false
    $content = Get-Content $actfile 2> $readerrorfile
    $readerror = Get-Content $readerrorfile
    if($readerror -match "Error"){
        echo "Error while reading from file $actfile"
        echo $readerror
        del $readerrorfile
        Write-Host "stopping execution"
        exit
    }else{
        del $readerrorfile
        if($content -match "keyword|regex"){
            echo "found in $actfile"
            $found = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我相当确定有一个更简单/更短的版本,也许是单行的。那么,最好的方法是什么grep

powershell grep

5
推荐指数
1
解决办法
8206
查看次数

Powershell 与 vbs 与 bat

我需要配置几台 Windows 7 机器(具有各种各样的任务 - 从设置屏幕保护程序、禁用服务、添加任务计划条目到设置 IPsec/防火墙规则)。

由于它将是几台机器,因此将其自动化而不是手动完成所有工作更有意义。

理想情况下,我想以两个文件结束:一个是脚本,另一个是 fe xml 保存特定设置的变量。

我的问题是 -哪种脚本环境最适合该任务?

我过去没有做过任何严肃的 Windows 脚本,所以这本质上是一个问题“我应该学习哪种脚本环境?

windows-7 powershell command-line vbscript

5
推荐指数
2
解决办法
4770
查看次数

非字母数字字符文件夹名称自动补全问题

我一直在使用 Windows 7 的命令行,并且有一些以非字母数字字符开头的文件夹。当我尝试使用制表符补全来完成文件夹名称时,引号内不包含首字母。示例:C:\Users\username\!example是我想进入的文件夹,但是当我输入时:

cd !
Run Code Online (Sandbox Code Playgroud)

并按<Tab>自动完成,它将完成

cd !"!example" 
Run Code Online (Sandbox Code Playgroud)

而不是预期的

cd "!example"
Run Code Online (Sandbox Code Playgroud)

除了更改文件夹名称之外,还有其他解决方法的想法吗?

编辑:我意识到我可以在输入后浏览整个列表cd,但我正在寻找一种方法来加快这个过程。我花了大量时间浏览这些文件夹。

更新:如果目录中有空格,也会发生这种情况。例如:“c:\Program Files”。为了继续使用制表符来完成,首先必须删除第二个引号。

C:\程序

按 T​​ab

“C:\Program Files”就是出现的内容。要导航到子目录,首先必须删除 Program Files 之后的引号,然后才能拼出下一个目录。

windows-7 powershell command-line

5
推荐指数
1
解决办法
549
查看次数

PowerShell 文件计数行为

我是 PowerShell 的新手,对以下行为有疑问。我正在尝试计算某种类型目录中的文件数。如果有的话,我会把它们复制到别处。这是我的测试文件夹的输出;该代码是从我发现的 MSDN 示例中修改的:

[PS]> Get-ChildItem c:\pstdump

Directory: C:\pstdump

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/12/2011   9:48 AM          0 blah.pst
-a---         2/12/2011   9:36 AM          0 New Text Document.txt
-a---         2/12/2011   9:36 AM         20 New WinRAR archive.rar

[PS]> (Get-ChildItem c:\pstdump).Count
3
[PS]>
Run Code Online (Sandbox Code Playgroud)

一切顺利;PowerShell 和我同意文件夹中有 3 个文件。现在,当我想选择特定的文件类型(例如 .PST 文件)时:

[PS]> Get-ChildItem c:\pstdump -filter "*.pst"

Directory: C:\pstdump

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/12/2011   9:48 AM          0 blah.pst

[PS]> (Get-ChildItem c:\pstdump -filter "*.pst").Count
[PS]>
Run Code Online (Sandbox Code Playgroud)

即使过滤器应该返回 1 …

powershell

5
推荐指数
1
解决办法
3980
查看次数

如何从 powershell 命令设置环境变量?

我想在 cmd.exe 中有一个包含星期几的环境变量。

当我运行这个命令时,我得到了我想要的结果。

C:\Users\tisc>powershell (get-date).dayofweek
Friday
Run Code Online (Sandbox Code Playgroud)

在这里,我试图将结果存储在环境变量中。

C:\Users\tisc>set dow = powershell (get-date).dayofweek
Run Code Online (Sandbox Code Playgroud)

但是当我试图得到它时,我没有得到我想要的字符串。

C:\Users\tisc>set dow
DoW=0
dow = powershell (get-date).dayofweek
Run Code Online (Sandbox Code Playgroud)

我的目标是将批处理文件中的变量用于某些备份脚本。

powershell command-line

5
推荐指数
1
解决办法
4297
查看次数

静默 Java 更新检查

如何在没有jusched.exe进程一直在后台运行并控制何时检查更新的情况下自动更新 Java ?

windows java powershell vbscript automatic-update

5
推荐指数
1
解决办法
6510
查看次数

如何附加 netsh 防火墙规则,而不仅仅是替换

我正在使用脚本在 netsh 中设置防火墙规则。

netsh.exe advfirewall firewall set rule name="Block External IP's" new remoteip="$($ipArray[0])"
Run Code Online (Sandbox Code Playgroud)

我认为使用该集合是为了附加规则而不是替换它。但是,它覆盖了列表中的前一个 ip,并将其替换为数组中的最后一个 ip。我怎样才能重写它以便它添加到规则而不是替换它们?非常感谢您的时间和帮助。

networking firewall powershell ip netsh

5
推荐指数
1
解决办法
5400
查看次数

启动进程后 Windows 启动 Powershell 脚本未关闭

我有一个适用于我的工作计算机(XP Professional 64 位)的 Powershell V2.0 启动脚本,如下所示:

start "C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE" -ArgumentList "/recycle"
sleep -S 2
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -ArgumentList "https://mail.google.com"
sleep -S 2
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -ArgumentList "-new-window https://www.google.com/calendar"
sleep -S 2
start "C:\Program Files (x86)\Skype\Phone\Skype.exe"
Run Code Online (Sandbox Code Playgroud)

睡眠是为了确保窗口以正确的顺序出现在任务栏上。我从快速启动上的快捷方式使用以下目标运行它:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\scripts\initialize.ps1
Run Code Online (Sandbox Code Playgroud)

(是的,这是 2.0:powershell -Version 2.0有效,同样如此-Version 1.0,但不是-Version 3.0

问题是,命令窗口保持打开状态,直到 Firefox 窗口关闭,这不是我想要的。当我运行脚本时查看进程资源管理器,会发生以下情况:

  1. powershell.exe 出现在 explorer.exe 下并出现 Powershell 窗口(奇怪的是黑色背景。但它不是 cmd.exe,因为当我调试脚本时,错误消息会显示为红色)。
  2. Outlook.exe 出现在 powershell.exe 下,并出现 Outlook 窗口。
  3. firefox.exe 出现在 powershell.exe 下,并出现一个 Firefox 窗口。
  4. 第二个 …

firefox powershell console windows-xp

5
推荐指数
1
解决办法
4207
查看次数

可能的 Windows 8 cmd 更改?

出于某种原因,我决定在我的开发机器上试用 Windows 8。到目前为止,一切都很好,直到我尝试启动具有一些自定义功能的 Powershell,包括PATHvcvars32.bat.

最初,脚本是从这里提取的/sf/ask/9670111/进行了一些更改以允许它在 x64 Powershell 实例中运行,所以它最终看起来像这样:

function Get-Batchfile($file) 
{
    $theCmd = "`"$file`" & set" 
    cmd /c $theCmd | Foreach-Object {
        $thePath, $theValue = $_.split('=')
        Set-Item -path env:$thePath -value $theValue
    }
}

function VsVars32($version = "10.0")
{
    $theKey = "HKLM:SOFTWARE\Wow6432Node\Microsoft\VisualStudio\" + $version
    $theVsKey = get-ItemProperty $theKey
    $theVsInstallPath = [System.IO.Path]::GetDirectoryName($theVsKey.InstallDir)
    $theVsToolsDir = [System.IO.Path]::GetDirectoryName($theVsInstallPath)
    $theVsToolsDir = [System.IO.Path]::Combine($theVsToolsDir, "Tools")
    $theBatchFile = [System.IO.Path]::Combine($theVsToolsDir, "vsvars32.bat")
    write-host $theBatchFile
    Get-Batchfile $theBatchFile
    [System.Console]::Title = "Visual Studio " + $version + " …
Run Code Online (Sandbox Code Playgroud)

powershell command-line windows-8

5
推荐指数
1
解决办法
4491
查看次数