我一直在将旧的批处理文件转换为 powershell 脚本并取得了不错的成功。但是......我无法弄清楚在这种情况下最好和最有效的方法是什么。
这是批处理脚本:
attrib -h -s *.* /s
del /s folder.jpg
del /s albumart*.jpg
del /s desktop.ini
@pause
Run Code Online (Sandbox Code Playgroud)
基本上它会通过我的音乐文件夹和子文件夹并删除可能存在的所有垃圾(我的音乐文件夹中有它)。
这样的东西会起作用吗(快速测试后它没有但是......)?
$currentfolder = split-path -parent $MyInvocation.MyCommand.Definition
Get-ChildItem -Path $currentfolder -Include folder.jpg, albumart*.jpg, desktop.ini -File -Recurse | foreach { $_.Delete()}
Run Code Online (Sandbox Code Playgroud)
回显已删除的文件名也很好。
编辑:我在这里添加了完整的解决方案:
$currentfolder = split-path -parent $MyInvocation.MyCommand.Definition
Get-ChildItem -Path $currentfolder -Include folder.jpg, albumart*.jpg, desktop.ini -File -Recurse | foreach { echo "Deleting: $_" ; $_.Delete()}
Run Code Online (Sandbox Code Playgroud) 使用PowerShell 5.0,有很多自定义选项。目前我已经创建了自己的配色方案。但我无法弄清楚如何永久设置这些颜色选项。
Set-PSReadlineOption -TokenKind Operator -ForegroundColor "Magenta"
Set-PSReadlineOption -TokenKind Command -ForegroundColor "DarkYellow"
Set-PSReadlineOption -TokenKind Parameter -ForegroundColor "Magenta"
Run Code Online (Sandbox Code Playgroud)
现在我将这些命令保存在Profile脚本中,以便它们会被自动加载,但是当 shell 被重置并且如果我想加载另一个Profile. 无论配置文件如何,背景颜色和其他可用选项都PowerShell UI保持一致。我想要同样的东西。
如何永久设置这些颜色选项PowerShell Console。
我使用的是 Windows 10,每当我运行 PowerShell 时,我几乎只需要在管理员模式下运行它,但是任务栏快捷方式似乎只能通过右键单击图标并选择Run as Administrator来启用此功能。
有没有办法让快捷方式本身简单地以管理员身份运行?
我想要cat一个文件并输出它输出的每一行的行号。
但是,在 PowerShell 中,cat输出一个数组。因此,问题实际上变成了:如何在输出到控制台时打印每个项目的索引......?
我试过这样的事情:
$k = cat foo.js
$k | foreach { $index = $k.IndexOf($_) + 1; write "$index : $_"; } | more
Run Code Online (Sandbox Code Playgroud)
它给了我一些奇怪的结果。一些行号重复。什么是优雅且更可靠的方法来做到这一点?
我有自动 telnet 服务器的批处理文件,我想用 PowerShell 做同样的事情
名为 Script.bat 的批处理文件:
:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs
Run Code Online (Sandbox Code Playgroud)
名为 SendKeys.vbs 的命令文件:
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""
Run Code Online (Sandbox Code Playgroud) 我尝试通过 npm 将私有 gitlab 存储库安装到另一个节点项目。命令是npm install --save gitlab:my-project#master
This failed with
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
Run Code Online (Sandbox Code Playgroud)
我有以下设置:Windows 10、git 和 Powershell。
直接在 Powershell 中使用 git 命令工作正常,例如git pull询问我的私钥的密码并且它工作正常。除了npm install似乎在内部使用 git的命令之外,所有其他 git 东西都可以正常工作。
如果我使用在 Windows 上随 git 安装的 git bash,我可以运行 npm 命令并安装私有存储库。它仅在 Powershell 中失败。
尽管如此,人们可以说:使用 git bash,但我以某种方式习惯了 PowerShell。:)
问题:我们有一台用于演示的大电视,它不完全是 1920×1080 像素。它关闭了,我无法控制它。我可以控制的是我连接到它的 Windows 笔记本电脑。我一直在寻找方法来显示我需要的任何内容,移动到投影仪,设置一个偏移位置以在电视上看起来不错,并设置相同的宽度/高度。
做了什么:我可以使用 Boe Prox 的 Get-Window 脚本(https://blogs.technet.microsoft.com/heyscriptingguy/2015/12/26/weekend-scripter-manage-window-placement-by-using-pinvoke/ ),而且效果很好,可以为我提供所需的尺寸和偏移量。但是,无论有没有管理员权限,他在 WinPosh 5 和 Posh 6 中都出现了我的设置窗口错误。其他潜在的解决方案产生了类似的错误,所以我决定继续使用 Prox 的脚本,因为按照我的标准,他是专家。
我正在评估这里提到的解决方案, /sf/ask/727483431/ -window/作为可能的解决方法。但是,不依赖任何第三方的 PowerShell 解决方案将是理想的。
问题是: 有没有人解决了如何在直接 v5 或 v6 PowerShell 中获取 Prox 先生的设置窗口或其他任何设置窗口位置和大小的方法?
Posh 5 中的错误消息:
Cannot convert argument "hWnd", with value: "System.Object[]", for "GetWindowRect" to type "System.IntPtr": "Cannot
convert the "System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At Z:\scripts\Set-Window.ps1:90 char:9
+ $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) …Run Code Online (Sandbox Code Playgroud) 在PowerShell的OpenSSH的网站有关于如何安装和使用OpenSSH很好的说明。
其中包括如何“配置默认的 ssh shell(可选)”
如果我选择使用 git-bash 而不是 powershell,-Value除了"/c"
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShellCommandOption -Value "/c" -PropertyType String -Force
Run Code Online (Sandbox Code Playgroud)
当我将默认 shell 设置为 git-bash 时,我得到一个空白屏幕。
我试过了"-c","-l -i"但没有一个工作,我仍然得到一个空白屏幕。
我正在尝试快速显示窗口事件日志中最后 ~ 天的所有事件,其中包含 Power Shell 中的某个字符串。
我找到了用于列出事件的 powershell 命令,但我基本上想为特定文本“GREP”它们。
我需要使用 powershell,因为目标是 Windows Server 2016 Hyper-V,但我认为能够在任何带有 powershell 的机器上快速搜索最近的事件也非常有用。
为了显示可用的日志,我运行:
PS C:\Users\Administrator> Get-EventLog -List
Max(K) Retain OverflowAction Entries Log
------ ------ -------------- ------- ---
20,480 0 OverwriteAsNeeded 1,113 Application
20,480 0 OverwriteAsNeeded 0 HardwareEvents
512 7 OverwriteOlder 0 Internet Explorer
20,480 0 OverwriteAsNeeded 0 Key Management Service
512 7 OverwriteOlder 1,539 Microsoft-ServerManagementExperience
20,480 0 OverwriteAsNeeded 28,667 Security
20,480 0 OverwriteAsNeeded 4,857 System
15,360 0 OverwriteAsNeeded 3,654 Windows PowerShell
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我的目标日志被称为应用程序 …
请帮我处理 PowerShell 环境变量。
我了解到 PowerShell 具有访问系统环境变量值的特殊语法。所以我试图执行:
$env:Temp
Run Code Online (Sandbox Code Playgroud)
在 Windows 10 上的 PowerShell 6.2.4 控制台中。输出很奇怪:
C:\Users\OD42B~1.BOR\AppData\Local\Temp
Run Code Online (Sandbox Code Playgroud)
它将我的 Windows 用户名缩短为 8.3 形式。问题是我无法使用该cd $env:temp命令,它显示以下错误:
cd : An object at the specified path C:\Users\OD42B~1.BOR does not exist.
Run Code Online (Sandbox Code Playgroud)
但是我可以cd %temp%在cmd终端上做。
我试过 Windows PowerShell 应用程序 ( %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe) 但cd $env:Temp也不起作用。
我已经检查了环境变量配置选项(系统属性\环境变量),TEMP并且TMP变量和变量都使用长格式的配置文件文件夹名称显示,例如c:\users\o.borolongprofilename\AppData\Local\Temp.
如何cd $env:Temp在 PowerShell 中使用非 8.3 配置文件名称?
PowerShellget-childitem env:Temp输出是C:\Users\OD42B~1.BOR\AppData\Local\Temp.
Cmdecho %temp%输出是相同的C:\Users\OD42B~1.BOR\AppData\Local\Temp(但 …
powershell ×10
windows ×4
ssh ×2
bash ×1
batch ×1
cat ×1
colors ×1
command-line ×1
event-log ×1
git ×1
git-bash ×1
linux ×1
openssh ×1
permissions ×1
telnet ×1
windows-10 ×1
windows-8 ×1