我想替换文本Powershell Startup
和CMD StartUp
下面的内容
# PowerShell
PS C:\Users\Admin>
# CMD
C:\Users\Admin>
和
PS >
CMD >
就像Python Start Up
这样只显示
>>>
这可能吗?
我想替换文本Powershell Startup
和CMD StartUp
下面的内容
# PowerShell
PS C:\Users\Admin>
# CMD
C:\Users\Admin>
和
PS >
CMD >
就像Python Start Up
这样只显示
>>>
这可能吗?
我正在创建一个带有基本变量的脚本,以通过 PS7 在 AD 中创建用户,而不是远程处理到 AD 并手动创建。它会回答几个问题,然后我的脚本将在最后运行以创建 AD 用户。
当我不知道 while 循环时,这是非常原始的起始块
$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)
$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
if ($newp1 -CEQ $newp2) {
Write-Host "Passwords matched" -ForegroundColor Green
} else {
Write-Host "Passwords incorrect - please reset in AD once user is
created" -ForegroundColor Red
}
我已经掌握了基础知识的所有内容,但我在密码循环功能方面遇到了困难。我一度让它循环,但它不会写主机“密码不匹配,请重试”。我现在已经玩了很多次了,但无法恢复到原来的样子,所以寻求帮助,并希望我犯了一个愚蠢的错误,因为我对代码视而不见。
我对 PowerShell 非常陌生,除了在线网络研讨会和书籍之外,我是自学的,我理解 While 的前提,但不是 100% 理解其中的要点。该脚本是在创建 AD 帐户之前创建的,完整的脚本是在您到达最后一个问题后创建它。之后它将运行 new-aduser 等。
while( $true){
$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)
$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
if ($newp1 -CEQ $newp2){
Write-Host "Password Accepted" -ForegroundColor Green
}
Break
}
catch{
Write-Host "Passwords do not match, please try again" -ForegroundColor Red
}
}
我想获取 PDF 并将每个页面提取为图像。我已经能够使用 ImageMagick 和 GhostScript 做到这一点,但结果质量非常差。我尝试了许多不同的输出选项,但没有任何运气。下面的脚本应该是相当不言自明的。它可以工作,但与打开 PDF 相比,图像质量确实令人失望。
# Extract each page from a PDF as a png using ImageMagick
# ImageMagick requires GhostScript for PDF manipulation so have to make sure that is installed
# Current install folder: C:\Program Files\ImageMagick-7.1.1-Q16-HDRI
# Chocolatey package does not inclued the 'identify.exe' command
# Path to the PDF file
$pdfFilePath = "C:\0\MyFile.pdf"
# Output directory for images
$outputDirectory = "C:\0"
# Image type to output to (tried jpg, png, tiff etc)
$imageExtension = "jpg"
# Check if running as Admin
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "Please run this script as an administrator."; exit }
# Check if Chocolatey is installed, if not, install it
if (!(Test-Path "$env:ProgramData\chocolatey")) { Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) }
# Check for magick.exe on path; if not installed, install ImageMagick
$imageMagickExePath = Get-ChildItem -Path "C:\Program Files\ImageMagick-*" -Filter "magick.exe" -Recurse | Select-Object -First 1 -ExpandProperty FullName
if (!(Get-Command "magick.exe" -ea silent) -and ($null -eq $imageMagickExePath)) { Write-Host "ImageMagick not found. Installing..."; choco install imagemagick -y }
if ($null -eq $imageMagickExePath) { Write-Host "Error: magick.exe not found at $imageMagickExePath"; exit }
Write-Host "magick.exe found at '$imageMagickExePath'"
# Check for gswin64.exe on path; if not installed, install GhostScript
$gsExePath = Get-ChildItem -Path "C:\Program Files\gs\gs*\bin" -Filter "gswin64.exe" -Recurse | Select-Object -First 1 -ExpandProperty FullName
if (!(Get-Command "gswin64.exe" -ea silent) -and ($null -eq $gsExePath)) { Write-Host "GhostScript not found. Installing..."; choco install ghostscript -y }
if ($null -eq $gsExePath) { Write-Host "Error: gswin64.exe not found, this is required by ImageMagick for PDF manipulation"; exit }
Write-Host "gswin64.exe found at '$gsExePath'"
# Add Ghostscript directory to the PATH temporarily so that ImageMagick can use it
$env:Path += ";$($gsExePath | Split-Path -Parent)"
# Create the output directory if it doesn't exist
if (-not (Test-Path $outputDirectory)) { New-Item -ItemType Directory -Force -Path $outputDirectory | Out-Null }
# Convert each page of the PDF to PNG
$imageNamePrefix = [System.IO.Path]::GetFileNameWithoutExtension($pdfFilePath)
$imageNamePrefix = $imageNamePrefix -replace '\s+', '_'
# Use ImageMagick's identify command to get the total number of pages in the PDF
$numberOfPages = (identify "$pdfFilePath" 2>$null | Measure-Object -Line).Lines
Write-Host "'$pdfFilePath' has $numberOfPages pages"
# Use ImageMagick's convert command to convert PDF
Start-Process $imageMagickExePath -ArgumentList "convert `"$pdfFilePath`" -density 600 -quality 100 -antialias -resize 300% `"$outputDirectory\$imageNamePrefix-%d.$imageExtension`"" -NoNewWindow -Wait
# Determine the maximum number of digits to normalise all page numbers to that length
$maxDigits = $numberOfPages.ToString().Length
# Normalize page numbers
for ($i = 0; $i -le $numberOfPages; $i++) {
$pageNumber = "{0:D$maxDigits}" -f $i
$oldFileName = Join-Path $outputDirectory "$imageNamePrefix-$i.$imageExtension"
$newFileName = Join-Path $outputDirectory "$imageNamePrefix-$pageNumber.$imageExtension"
if ((Test-Path $oldFileName) -and !(Test-Path $newFileName)) { Rename-Item -Path $oldFileName -NewName $newFileName }
}
# Remove the Ghostscript directory from the PATH
$env:Path = $env:Path -replace [regex]::Escape(";"+($gsExePath | Split-Path -Parent))
# command-line tools for manipulating PDFs:
# https://libgen.rs/search.php?req=pdf+hacks&lg_topic=libgen&open=0&view=simple&res=25&phrase=1&column=defs
# pdftk (PDF Toolkit): pdftk is a command-line tool for manipulating PDF files. It can merge, split, rotate, watermark, and decrypt PDF files.
# QPDF: QPDF is another command-line tool for structural, content-preserving transformation of PDF files. It's particularly useful for linearizing PDFs, decrypting, and compressing them.
# Poppler Utilities (pdftohtml, pdftotext, pdfimages): Poppler is a PDF rendering library and its utilities provide command-line tools for converting PDFs to various formats such as HTML, text, and images. pdftohtml converts PDF to HTML, pdftotext converts PDF to plain text, and pdfimages extracts images from PDFs.
# Ghostscript: Ghostscript is a suite of software based on an interpreter for Adobe Systems' PostScript and Portable Document Format (PDF) page description languages. It can be used for a wide variety of tasks including converting PDFs to various formats, merging PDFs, and more.
# MuPDF: MuPDF is a lightweight PDF, XPS, and E-book viewer. It also includes command-line tools for extracting text and images from PDFs.
# PDFMiner: PDFMiner is a tool for extracting information from PDF documents. It includes a command-line tool for extracting text, images, and other content from PDFs.
function main {
$options = @('option1', 'option2', 'option3')
CLS; $n = 1
$options | foreach { "[{0}] {1}" -f $n, $_; $n++ }
choice /c 123 /m "Choose:"
"opt$lastexitcode"
pause; main
}
function opt1 { "this is option one" }
function opt2 { "this is option two" }
function opt3 { "this is option three" }
main
如果可能的话,直接调用其他函数并输入名称函数而不使用if或switch语句?
打算从此启动vim
PS C:\temp> get-command vim | select name,source
Name Source
---- ------
vim.bat C:\Users\noam\AppData\Local\Microsoft\WindowsApps\vim.bat
我不小心进入了vm
而不是,随之vim
而来的是很多令人头疼的事情:
C:\temp>pwsh -noprofile
PowerShell 7.4.1
PS C:\temp> vm
Get-VM: 29-Mar-24 16:28:14 Get-VM You are not currently connected to any servers. Please connect first using a Connect cmdlet.
PS C:\temp> get-command vm
Get-Command: The term 'vm' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\temp> get-alias vm
Get-Alias: This command cannot find a matching alias because an alias with the name 'vm' does not exist.
PS C:\temp> cmd /c where vm
INFO: Could not find files for the given pattern(s).
如何查找并删除看似从 VMware PowerCLI映射vm
到的配置?Get-VM
PS C:\temp> get-command get-vm | select Name,Source
Name Source
---- ------
Get-VM VMware.VimAutomation.Core
我一定是错过了一些愚蠢的事情。我本以为上面的调用where.exe
应该发现了与我的路径相关的任何内容。无论如何,设置后行为不会改变$env:PATH = $null
。
作为一个拼凑,我可以明确地重新定义vm
viaSet-Alias
到一些不存在的命令。但我想了解为什么在执行时看不到类似下面的内容vm
。我忽略了什么?
PS C:\temp> doesnotexist
doesnotexist: The term 'doesnotexist' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
无论是否VMware.VimAutomation.Core
显式导入模块,上述结果都可以重现。
我正在关注此处的教程:https://www.youtube.com/watch?v =ZMfHaUkhfc0 我大约在 15:00 左右。我已在管理员帐户的 PowerShell 中安装了 WSL2。我以普通用户身份登录。我安装了 Docker Desktop 和 Ubuntu。我重新启动。然后我再次以管理员帐户打开 Powershell,但它没有看到我安装的 Ubuntu 发行版或我安装的 Docker Desktop。
PS C:\windows\system32> wsl -l -v
Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Microsoft Store:
https://aka.ms/wslstore
PS C:\windows\system32> wsl.exe -d Ubuntu
There is no distribution with the supplied name.
PS C:\windows\system32> wsl.exe --install Ubuntu
Copyright (c) Microsoft Corporation. All rights reserved.
Usage: wsl.exe [Argument] [Options...] [CommandLine]
Arguments for running Linux binaries:
If no command line is provided, wsl.exe launches the default shell.
--exec, -e <CommandLine>
Execute the specified command without using the default Linux shell.
--
Pass the remaining command line as is.
同样,我使用常规(非管理员)帐户,并且能够在 Windows 中打开 Ubuntu 应用程序和 Docker 桌面应用程序,但我以管理员身份运行的 Powershell 会话也看不到。我该如何解决?为什么wsl.exe --install Ubuntu
调用失败并显示简单的使用消息?
我有一些命令只能在前缀为&
.
它们都是异步运行的。
示例脚本:
& 'C:\Program Files\SomeApp\SomeAppUninstall.exe'/silent
& 'C:\Users\bob\Downloads\SomeApp.exe' /silent
& 'C:\Program Files\someApp.exe' /openGui
这里发生的情况是最后一个命令将首先发生,因为它是最快的,并且程序仍然安装,前两个命令将同时运行,导致冲突。
显然我需要它们按顺序运行。
由于某种原因,这些命令只有在带有前缀的情况下才有效&
,或者我收到错误,我怀疑这是因为它们有开关。
我该如何解决这个问题?
这是我的 PowerShell 代码:
$Phones = @('iPhone 12', 'Samsung S5', 'Nokia 7')
$Manufs = @('US', 'South Korea', 'Finlandia')
$n = 0
$Phones | foreach {
"$($_) : $($Manufs[$n])"
$n++
}
输出:
iPhone 12: US
Samsung S5: South Korea
Nokia 7: Finlandia
如何在之前对齐空格/制表符,:
使其变为:
iPhone 12 : US
Samsung S5 : South Korea
Nokia 7 : Finlandia
有人可以向我解释这种行为吗?怎么了?
PS C:\Scripts\Customer_Shares_Permissions> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.5328
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.5328
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\Scripts\Customer_Shares_Permissions> Get-Content .\test.ps1
# Test Write-Host redirecting
Write-Host "Dies ist eine Ausgabe"
Write-Host "Per Write-Host"
Write-Host "auf die Konsole"
Write-Host "Bitte prüfe den redirect in eine Datei"
PS C:\Scripts\Customer_Shares_Permissions> .\test.ps1
Dies ist eine Ausgabe
Per Write-Host
auf die Konsole
Bitte prüfe den redirect in eine Datei
PS C:\Scripts\Customer_Shares_Permissions> .\test.ps1 > test.txt
Dies ist eine Ausgabe
Per Write-Host
auf die Konsole
Bitte prüfe den redirect in eine Datei
PS C:\Scripts\Customer_Shares_Permissions> Get-Content .\test.txt
PS C:\Scripts\Customer_Shares_Permissions>
文件test.txt
已创建,但为空,并且输出仍显示在控制台上,而不是定向到文件中。
这不是我所期望的行为。我的思维问题出在哪里?