1
0
wiki/Technology/GettingStarted/命令行工具/PowerShell/PowerShell-高级.html

731 lines
626 KiB
HTML
Raw Normal View History

2024-09-06 17:53:58 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>PowerShell 高级</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="PowerShell 高级"/><meta property="og:description" content="PowerShell 高级."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="PowerShell 高级."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/GettingStarted/命令行工具/PowerShell/PowerShell-高级"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"
<h3 id="管理文件和文件夹">管理文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了丰富的命令来管理文件和文件夹,包括创建、删除、复制、移动和重命名文件和文件夹。</p>
<h4 id="创建文件和文件夹">创建文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>New-Item</code> 命令来创建文件和文件夹:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建文件夹</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ItemType Directory</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ItemType File</span></span></code></pre></figure>
<h4 id="删除文件和文件夹">删除文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#删除文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Remove-Item</code> 命令来删除文件和文件夹:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除文件夹及其所有内容</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Recurse</span></span></code></pre></figure>
<h4 id="复制和移动文件">复制和移动文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#复制和移动文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Copy-Item</code><code>Move-Item</code> 命令来复制和移动文件:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 复制文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Copy-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Destination </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\copy_of_file.txt&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 移动文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Move-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Destination </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\moved_file.txt&quot;</span></span></code></pre></figure>
<h4 id="重命名文件和文件夹">重命名文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#重命名文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Rename-Item</code> 命令来重命名文件和文件夹:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 重命名文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Rename-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\moved_file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">NewName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;renamed_file.txt&quot;</span></span></code></pre></figure>
<h3 id="文件内容的读取和写入">文件内容的读取和写入<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#文件内容的读取和写入" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了便捷的方法来读取和写入文件内容。</p>
<h4 id="读取文件内容">读取文件内容<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#读取文件内容" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Get-Content</code> 命令来读取文件内容:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 读取文件内容</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Content</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span></code></pre></figure>
<h4 id="写入文件内容">写入文件内容<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#写入文件内容" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Set-Content</code><code>Add-Content</code> 命令来写入和追加文件内容:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 写入文件内容</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Content</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Value </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello, World!&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 追加文件内容</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Add-Content</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Value </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;This is an appended line.&quot;</span></span></code></pre></figure>
<h3 id="搜索文件和文件夹">搜索文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#搜索文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以使用 <code>Get-ChildItem</code> 命令来搜索文件和文件夹。支持通配符和递归搜索。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 搜索当前目录下的所有文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ChildItem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 搜索当前目录及其子目录下的所有文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ChildItem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Recurse</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用通配符搜索特定类型的文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ChildItem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\*.txt&quot;</span></span></code></pre></figure>
<h3 id="文件属性和权限管理">文件属性和权限管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#文件属性和权限管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以使用 <code>Get-Item</code><code>Set-ItemProperty</code> 命令来获取和设置文件属性,以及使用 <code>Get-Acl</code><code>Set-Acl</code> 命令来管理文件权限。</p>
<h4 id="获取和设置文件属性">获取和设置文件属性<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取和设置文件属性" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取文件属性</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$file </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$file.Attributes</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置文件为只读</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-ItemProperty</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name Attributes </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Value ReadOnly</span></span></code></pre></figure>
<h4 id="管理文件权限">管理文件权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理文件权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取文件权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置文件权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl.SetAccessRuleProtection(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$true</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> $false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 禁用继承</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$rule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.FileSystemAccessRule(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Everyone&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Read&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl.AddAccessRule($rule)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span></code></pre></figure>
<h3 id="压缩和解压文件">压缩和解压文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#压缩和解压文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以使用 <code>Compress-Archive</code><code>Expand-Archive</code> 命令来压缩和解压文件。</p>
<h4 id="压缩文件">压缩文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#压缩文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 压缩文件到 zip</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Compress-Archive</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DestinationPath </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\archive.zip&quot;</span></span></code></pre></figure>
<h4 id="解压文件">解压文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#解压文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 解压 zip 文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Expand-Archive</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\archive.zip&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DestinationPath </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\Unzipped&quot;</span></span></code></pre></figure>
<h3 id="管理临时文件和文件夹">管理临时文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理临时文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了一些命令来管理临时文件和文件夹。</p>
<h4 id="创建临时文件和文件夹">创建临时文件和文件夹<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建临时文件和文件夹" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建临时文件</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$tempFile </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">System.IO.Path</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::GetTempFileName()</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $tempFile </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ItemType File</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建临时文件夹</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$tempFolder </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">System.IO.Path</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::GetTempPath() </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">System.Guid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::NewGuid().ToString()</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $tempFolder </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ItemType Directory</span></span></code></pre></figure>
<h2 id="注册表管理">注册表管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#注册表管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 可以读取和修改 Windows 注册表项,用于系统配置和管理。注册表操作涉及到读取、修改、新建和删除注册表项,以及管理注册表值和权限。</p>
<h3 id="注册表概述">注册表概述<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#注册表概述" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Windows 注册表是一个层次化数据库,存储了有关系统、硬件、用户配置和软件的信息。注册表包含多个根键,每个根键存储不同类型的信息:</p>
<ul>
<li><strong>HKEY_LOCAL_MACHINE (HKLM)</strong>:存储计算机的硬件、软件、操作系统和安全性等全局设置,对所有用户生效。</li>
<li><strong>HKEY_CURRENT_USER (HKCU)</strong>:存储当前登录用户的配置信息和用户特定的设置。</li>
<li><strong>HKEY_CLASSES_ROOT (HKCR)</strong>:存储文件类型关联和 OLE 对象的配置信息。</li>
<li><strong>HKEY_USERS (HKU)</strong>:存储计算机上所有用户的配置信息,每个用户对应一个子键。</li>
<li><strong>HKEY_CURRENT_CONFIG (HKCC)</strong>:存储当前硬件配置文件的信息。</li>
</ul>
<div class="table-container"><table><thead><tr><th>根键</th><th>常见子键示例</th></tr></thead><tbody><tr><td><strong>HKEY_LOCAL_MACHINE (HKLM)</strong></td><td><code>SYSTEM</code>:系统配置信息,包括硬件设置、驱动程序和服务。</td></tr><tr><td></td><td><code>SOFTWARE</code>:安装在计算机上的软件和应用程序的信息。</td></tr><tr><td></td><td><code>SECURITY</code>:安全策略和权限设置。</td></tr><tr><td></td><td><code>SAM</code>:安全帐户管理数据库的配置信息。</td></tr><tr><td></td><td><code>HARDWARE</code>:硬件抽象层和即插即用管理的信息。</td></tr><tr><td><strong>HKEY_CURRENT_USER (HKCU)</strong></td><td><code>Software</code>:用户安装的软件和应用程序的配置信息。</td></tr><tr><td></td><td><code>Control Panel</code>:用户的控制面板设置,如桌面背景和显示设置。</td></tr><tr><td></td><td><code>Environment</code>:用户环境变量。</td></tr><tr><td></td><td><code>Network</code>:网络连接和配置的信息。</td></tr><tr><td></td><td><code>Printers</code>:用户的打印机和打印设置。</td></tr><tr><td><strong>HKEY_CLASSES_ROOT (HKCR)</strong></td><td><code>*\shell</code>:定义了所有文件类型的右键菜单命令。</td></tr><tr><td></td><td><code>file</code>:特定文件类型的关联设置。</td></tr><tr><td></td><td><code>CLSID</code>:类标识符的配置信息。</td></tr><tr><td></td><td><code>Interface</code>:接口标识符的配置信息。</td></tr><tr><td><strong>HKEY_USERS (HKU)</strong></td><td><code>S-1-5-18</code>LocalSystem 账户的设置。</td></tr><tr><td></td><td><code>S-1-5-19</code>LocalService 账户的设置。</td></tr><tr><td></td><td><code>S-1-5-20</code>NetworkService 账户的设置。</td></tr><tr><td></td><td><code>S-1-5-21-&lt;User-SID></code>:特定用户的配置信息。</td></tr><tr><td><strong>HKEY_CURRENT_CONFIG (HKCC)</strong></td><td><code>System</code>:系统硬件配置文件。</td></tr><tr><td></td><td><code>Software</code>:与当前硬件配置相关的软件设置。</td></tr></tbody></table></div>
<h3 id="读取和修改注册表项">读取和修改注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#读取和修改注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="读取注册表项">读取注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#读取注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Get-ItemProperty</code> 命令读取注册表项的值。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 读取注册表项值</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ItemProperty</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer&quot;</span></span></code></pre></figure>
<h4 id="修改注册表项">修改注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#修改注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Set-ItemProperty</code> 命令修改注册表项的值。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 修改注册表项值</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-ItemProperty</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Maximize&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Value </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span></span></code></pre></figure>
<h4 id="新建和删除注册表项">新建和删除注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#新建和删除注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>New-Item</code><code>Remove-Item</code> 命令来新建和删除注册表项。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 新建注册表项</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除注册表项</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Recurse</span></span></code></pre></figure>
<h4 id="新建和删除注册表值">新建和删除注册表值<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#新建和删除注册表值" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>New-ItemProperty</code><code>Remove-ItemProperty</code> 命令来新建和删除注册表值。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 新建注册表值</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-ItemProperty</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyValue&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Value </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;TestValue&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PropertyType String</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除注册表值</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-ItemProperty</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyValue&quot;</span></span></code></pre></figure>
<h3 id="管理注册表权限">管理注册表权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理注册表权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以使用 <code>Get-Acl</code><code>Set-Acl</code> 命令来获取和设置注册表项的权限。</p>
<h4 id="获取注册表项权限">获取注册表项权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取注册表项权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取注册表项权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl</span></span></code></pre></figure>
<h4 id="设置注册表项权限">设置注册表项权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置注册表项权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置注册表项权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$rule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.RegistryAccessRule(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Everyone&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;FullControl&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl.SetAccessRule($rule)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU:\Software\MyCompany&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span></code></pre></figure>
<h3 id="备份和还原注册表项">备份和还原注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#备份和还原注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以使用 <code>reg.exe</code> 命令来备份和还原注册表项。</p>
<h4 id="备份注册表项">备份注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#备份注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 备份注册表项到文件</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">reg export </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;HKCU\Software\MyCompany&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\Backup\MyCompany.reg&quot;</span></span></code></pre></figure>
<h4 id="还原注册表项">还原注册表项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#还原注册表项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 从文件还原注册表项</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">reg import </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Backup\MyCompany.reg&quot;</span></span></code></pre></figure>
<h2 id="进程和服务管理">进程和服务管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#进程和服务管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 提供了强大的功能来管理系统进程和 Windows 服务。</p>
<h3 id="管理系统进程">管理系统进程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理系统进程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="获取进程信息">获取进程信息<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取进程信息" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Get-Process</code> 命令来获取系统中运行的所有进程的信息,或获取特定进程的信息。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有进程信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取特定进程信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span></span></code></pre></figure>
<h4 id="启动和停止进程">启动和停止进程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动和停止进程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Start-Process</code> 命令来启动进程,使用 <code>Stop-Process</code> 命令来停止进程。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动进程</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">FilePath </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad.exe&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停止进程</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span></span></code></pre></figure>
<h4 id="获取和管理进程详细信息">获取和管理进程详细信息<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取和管理进程详细信息" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以获取进程的详细信息如内存使用情况、CPU 使用率等,还可以暂停和恢复进程。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取进程的详细信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Select-Object</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Property Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Id</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> CPU</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> WS</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 暂停进程</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Suspend-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 恢复进程</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Resume-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span></span></code></pre></figure>
<h3 id="管理-windows-服务">管理 Windows 服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理-windows-服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了全面的命令来管理 Windows 服务,包括获取服务信息、启动和停止服务、设置服务启动类型等。</p>
<h4 id="获取服务信息">获取服务信息<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取服务信息" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Get-Service</code> 命令来获取系统中的所有服务的信息,或获取特定服务的信息。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有服务信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Service</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取特定服务信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span></span></code></pre></figure>
<h4 id="启动和停止服务">启动和停止服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动和停止服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Start-Service</code><code>Stop-Service</code> 命令来启动和停止服务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停止服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span></span></code></pre></figure>
<h4 id="重启服务">重启服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#重启服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Restart-Service</code> 命令来重启服务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 重启服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Restart-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span></span></code></pre></figure>
<h4 id="设置服务的启动类型">设置服务的启动类型<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置服务的启动类型" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Set-Service</code> 命令来设置服务的启动类型,例如设置为自动启动或手动启动。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将服务设置为自动启动</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">StartupType Automatic</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将服务设置为手动启动</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;wuauserv&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">StartupType Manual</span></span></code></pre></figure>
<h4 id="管理服务依赖项">管理服务依赖项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理服务依赖项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>sc.exe</code> 命令来设置服务的依赖项,使服务在启动时依赖于其他服务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置服务依赖于其他服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> config </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> depend</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;OtherService&quot;</span></span></code></pre></figure>
<h3 id="windows-服务管理">Windows 服务管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#windows-服务管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Windows 服务是运行在 Windows 操作系统上的长期运行进程,通常在系统启动时自动启动,并在后台运行。使用 PowerShell 可以方便地创建、管理和配置 Windows 服务。</p>
<h4 id="创建-windows-服务">创建 Windows 服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建-windows-服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>创建 Windows 服务通常需要以下步骤:</p>
<ol>
<li>编写服务应用程序(通常使用 C# 或其他编程语言)。</li>
<li>使用 PowerShell 注册服务。</li>
</ol>
<h5 id="编写服务应用程序">编写服务应用程序<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#编写服务应用程序" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>你需要编写一个可以作为服务运行的应用程序。以下是一个简单的 C# 服务示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="c#" data-theme="github-light github-dark"><code data-language="c#" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">csharp复制代码using</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> System</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">using</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> System</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ServiceProcess</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">using</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> System</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">IO</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyService</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> : </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ServiceBase</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> private</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> FileSystemWatcher</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> watcher</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyService</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ServiceName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;MyService&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> protected</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> override</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> OnStart</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">string</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[] </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">args</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> watcher </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> new</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> FileSystemWatcher</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> watcher.Path </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">PathToWatch&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> watcher.Created </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> OnChanged;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> watcher.EnableRaisingEvents </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> File.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">AppendAllText</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ServiceLog.txt&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Service started at &quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> +</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> DateTime.Now </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Environment.NewLine);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> private</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> OnChanged</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">object</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> sender</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">FileSystemEventArgs</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> e</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> File.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">AppendAllText</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ServiceLog.txt&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">$&quot;File created: {</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">FullPath</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">} at {</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DateTime</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Now</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">}&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> +</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Environment.NewLine);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> protected</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> override</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> OnStop</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> watcher.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Dispose</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> File.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">AppendAllText</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ServiceLog.txt&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Service stopped at &quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> +</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> DateTime.Now </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Environment.NewLine);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ServiceBase.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">new</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyService</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">());</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<p>编译此 C# 代码,生成一个可执行文件(如 <code>MyService.exe</code>)。</p>
<h5 id="使用-powershell-注册服务">使用 PowerShell 注册服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-powershell-注册服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>编写完服务应用程序后,可以使用 PowerShell 注册服务:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义服务名称和路径</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;MyService&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceExePath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\Path\To\MyService.exe&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 sc.exe 创建服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> create $serviceName binPath</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $serviceExePath</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查服务状态</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span></code></pre></figure>
<h3 id="配置和管理-windows-服务">配置和管理 Windows 服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置和管理-windows-服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>使用 PowerShell可以方便地管理 Windows 服务,包括启动、停止、重启和删除服务,以及配置服务的启动类型和依赖项。</p>
<h4 id="启动和停止服务-1">启动和停止服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动和停止服务-1" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停止服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span></span></code></pre></figure>
<h4 id="重启服务-1">重启服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#重启服务-1" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 重启服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Restart-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span></span></code></pre></figure>
<h4 id="删除服务">删除服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#删除服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停止并删除服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> delete </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span></span></code></pre></figure>
<h4 id="修改服务启动类型">修改服务启动类型<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#修改服务启动类型" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将服务设置为自动启动</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">StartupType Automatic</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将服务设置为手动启动</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">StartupType Manual</span></span></code></pre></figure>
<h4 id="设置服务依赖项">设置服务依赖项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置服务依赖项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置服务依赖于其他服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> config </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyService&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> depend</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;OtherService&quot;</span></span></code></pre></figure>
<h3 id="示例脚本">示例脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#示例脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>以下是一个完整的示例脚本,用于创建、启动、检查、重启和删除服务:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 服务名称和路径</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;MyService&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceExePath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\Path\To\MyService.exe&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> create $serviceName binPath</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $serviceExePath</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Service </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> created.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Service </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> started.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查服务状态</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceStatus </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Service </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> status: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceStatus.Status</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 重启服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Restart-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Service </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> restarted.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name $serviceName</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sc.exe</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> delete $serviceName</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Service </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$serviceName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> deleted.&quot;</span></span></code></pre></figure>
<h2 id="用户和权限管理">用户和权限管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#用户和权限管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 提供了管理用户账户、组和权限的功能,帮助管理员高效地管理 Windows 系统的安全和用户权限。</p>
<h3 id="用户和组的管理">用户和组的管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#用户和组的管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了一系列命令来管理本地用户和组,包括创建、删除和修改用户和组的属性。</p>
<h4 id="创建和删除用户">创建和删除用户<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建和删除用户" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>New-LocalUser</code> 命令可以创建新用户,使用 <code>Remove-LocalUser</code> 命令可以删除用户。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建新用户</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-LocalUser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewUser&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Password (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ConvertTo-SecureString</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;P@ssw0rd&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AsPlainText </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Force) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">FullName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;New User&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Description </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;This is a new user.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除用户</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-LocalUser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewUser&quot;</span></span></code></pre></figure>
<h4 id="创建和删除组">创建和删除组<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建和删除组" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>New-LocalGroup</code> 命令可以创建新组,使用 <code>Remove-LocalGroup</code> 命令可以删除组。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建新组</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-LocalGroup</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewGroup&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Description </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;This is a new group.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除组</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-LocalGroup</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewGroup&quot;</span></span></code></pre></figure>
<h4 id="将用户添加到组">将用户添加到组<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#将用户添加到组" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>Add-LocalGroupMember</code> 命令可以将用户添加到组,使用 <code>Remove-LocalGroupMember</code> 命令可以将用户从组中移除。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将用户添加到组</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Add-LocalGroupMember</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Group </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Administrators&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Member </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewUser&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将用户从组中移除</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-LocalGroupMember</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Group </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Administrators&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Member </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewUser&quot;</span></span></code></pre></figure>
<h4 id="查看用户和组信息">查看用户和组信息<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#查看用户和组信息" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>Get-LocalUser</code> 命令可以查看用户信息,使用 <code>Get-LocalGroup</code> 命令可以查看组信息。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看所有本地用户</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-LocalUser</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看特定用户信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-LocalUser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;NewUser&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看所有本地组</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-LocalGroup</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看特定组信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-LocalGroup</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Administrators&quot;</span></span></code></pre></figure>
<h3 id="权限设置">权限设置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#权限设置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了管理文件和文件夹权限的功能,可以查看和修改文件系统 ACL访问控制列表</p>
<h4 id="查看文件和文件夹权限">查看文件和文件夹权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#查看文件和文件夹权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>Get-Acl</code> 命令可以查看文件和文件夹的权限。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看文件权限</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看文件夹权限</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span></span></code></pre></figure>
<h4 id="修改文件和文件夹权限">修改文件和文件夹权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#修改文件和文件夹权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Set-Acl</code> 命令来修改文件和文件夹的权限。需要先获取现有的 ACL然后添加或修改权限规则最后应用新的 ACL。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取现有权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义新的权限规则</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$permission </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;DOMAIN\Username&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;FullControl&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$accessRule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.FileSystemAccessRule $permission</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 添加权限规则</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl.SetAccessRule($accessRule)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 应用新的权限设置</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span></code></pre></figure>
<h4 id="删除文件和文件夹权限">删除文件和文件夹权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#删除文件和文件夹权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以删除特定的权限规则。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取现有权限</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义要删除的权限规则</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$permission </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;DOMAIN\Username&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;FullControl&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$accessRule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.FileSystemAccessRule $permission</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除权限规则</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$acl.RemoveAccessRule($accessRule)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 应用新的权限设置</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span></code></pre></figure>
<h4 id="递归修改文件夹权限">递归修改文件夹权限<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#递归修改文件夹权限" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以递归修改文件夹及其子文件夹和文件的权限。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取文件夹及其子文件夹和文件</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$items </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-ChildItem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Example&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Recurse</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 修改每个项目的权限</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($item </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $items) {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $item.FullName</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $accessRule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.FileSystemAccessRule (</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;DOMAIN\Username&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Modify&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;ContainerInherit,ObjectInherit&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;None&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $acl.SetAccessRule($accessRule)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $item.FullName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h3 id="远程用户和权限管理">远程用户和权限管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#远程用户和权限管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 还提供了远程管理用户和权限的功能,通过 PowerShell Remoting 可以管理远程计算机上的用户和权限。</p>
<h4 id="启用-powershell-remoting">启用 PowerShell Remoting<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启用-powershell-remoting" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启用 PowerShell Remoting</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Enable-PSRemoting</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Force</span></span></code></pre></figure>
<h4 id="远程执行命令">远程执行命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#远程执行命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>可以使用 <code>Invoke-Command</code> 命令在远程计算机上执行用户和权限管理命令。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在远程计算机上创建用户</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Invoke-Command</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputer&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ScriptBlock {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-LocalUser</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteUser&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Password (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ConvertTo-SecureString</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;P@ssw0rd&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AsPlainText </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Force) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">FullName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Remote User&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Description </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;This is a remote user.&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在远程计算机上设置文件权限</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Invoke-Command</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputer&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ScriptBlock {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $acl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\RemoteExample\file.txt&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $permission </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;DOMAIN\Username&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;FullControl&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Allow&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $accessRule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.Security.AccessControl.FileSystemAccessRule $permission</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $acl.SetAccessRule($accessRule)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Set-Acl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\RemoteExample\file.txt&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AclObject $acl</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h2 id="网络管理">网络管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#网络管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 提供了强大的工具来管理和配置网络包括查看和设置网络适配器、IP 地址、DNS 服务器以及测试网络连接。还可以通过 PowerShell 进行远程管理。</p>
<h3 id="网络配置">网络配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#网络配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="获取和设置网络配置">获取和设置网络配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取和设置网络配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PowerShell 提供了丰富的 cmdlet 来获取和设置网络配置,包括查看网络适配器信息、配置 IP 地址等。</p>
<h5 id="获取网络适配器信息">获取网络适配器信息<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取网络适配器信息" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Get-NetAdapter</code> 命令可以查看所有网络适配器的信息,使用 <code>Get-NetIPAddress</code> 命令可以获取网络配置。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有网络适配器信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetAdapter</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取特定网络适配器信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetAdapter</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span></span></code></pre></figure>
<h5 id="获取网络配置">获取网络配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取网络配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有网络适配器的 IP 配置</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetIPAddress</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取特定网络适配器的 IP 配置</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetIPAddress</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span></span></code></pre></figure>
<h5 id="设置网络配置">设置网络配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置网络配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>New-NetIPAddress</code><code>Set-DnsClientServerAddress</code> 命令可以配置静态 IP 地址和 DNS 服务器。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置静态 IP 地址</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetIPAddress</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">IPAddress </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;192.168.1.100&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PrefixLength </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">24</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DefaultGateway </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;192.168.1.1&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置 DNS 服务器</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-DnsClientServerAddress</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ServerAddresses </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;8.8.8.8&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;8.8.4.4&quot;</span></span></code></pre></figure>
<h5 id="设置-dhcp">设置 DHCP<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置-dhcp" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Remove-NetIPAddress</code><code>Set-DnsClientServerAddress</code> 命令可以将网络配置改为 DHCP。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 移除静态 IP 地址以启用 DHCP</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-NetIPAddress</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Confirm:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$false</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置 DNS 服务器为自动获取</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-DnsClientServerAddress</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ResetServerAddresses</span></span></code></pre></figure>
<h4 id="配置防火墙">配置防火墙<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置防火墙" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PowerShell 还可以用于配置防火墙规则。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看现有防火墙规则</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetFirewallRule</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 新建入站规则允许 HTTP 流量</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetFirewallRule</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DisplayName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Allow HTTP&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Direction Inbound </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Protocol TCP </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">LocalPort </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action Allow</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 新建出站规则阻止 FTP 流量</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetFirewallRule</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DisplayName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Block FTP&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Direction Outbound </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Protocol TCP </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">RemotePort </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">21</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action Block</span></span></code></pre></figure>
<h3 id="测试网络连接">测试网络连接<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#测试网络连接" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了多种方法来测试网络连接,例如使用 <code>Test-Connection</code><code>Test-NetConnection</code> cmdlet。</p>
<h4 id="测试网络连通性">测试网络连通性<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#测试网络连通性" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>Test-Connection</code> 命令可以测试网络连接,类似于 <code>ping</code> 命令。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 Test-Connection 测试网络连接</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Test-Connection</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;www.google.com&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Count </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 Test-NetConnection 测试特定端口的网络连接</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Test-NetConnection</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;www.google.com&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Port </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span></span></code></pre></figure>
<h3 id="远程管理">远程管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#远程管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了强大的远程管理功能,可以通过 WinRMWindows Remote Management和 SSH 进行远程管理。</p>
<h4 id="使用-powershell-进行远程管理winrm-ssh">使用 PowerShell 进行远程管理WinRM, SSH<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-powershell-进行远程管理winrm-ssh" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<h5 id="启用-winrm">启用 WinRM<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启用-winrm" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启用 WinRM 服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Enable-PSRemoting</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Force</span></span></code></pre></figure>
<h5 id="配置-ssh">配置 SSH<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置-ssh" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 安装 OpenSSH 客户端和服务器Windows 10 及以上版本)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Add-WindowsCapability</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Online </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name OpenSSH.Client</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Add-WindowsCapability</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Online </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name OpenSSH.Server</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动并配置 SSH 服务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-Service</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> sshd</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name sshd </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">StartupType </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Automatic'</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置防火墙规则允许 SSH 连接</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetFirewallRule</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name sshd </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DisplayName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'OpenSSH Server (sshd)'</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Enabled True </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Direction Inbound </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Protocol TCP </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action Allow </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">LocalPort </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">22</span></span></code></pre></figure>
<h4 id="使用-invoke-command-和-enter-pssession">使用 Invoke-Command 和 Enter-PSSession<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-invoke-command-和-enter-pssession" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<h5 id="使用-invoke-command-在远程计算机上执行命令">使用 <code>Invoke-Command</code> 在远程计算机上执行命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-invoke-command-在远程计算机上执行命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 在远程计算机上执行命令</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Invoke-Command</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputerName&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ScriptBlock { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用凭证执行命令</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$cred </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Credential</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Invoke-Command</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputerName&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Credential $cred </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ScriptBlock { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span></code></pre></figure>
<h5 id="使用-enter-pssession-进入远程会话">使用 <code>Enter-PSSession</code> 进入远程会话<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-enter-pssession-进入远程会话" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 进入远程计算机的 PowerShell 会话</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Enter-PSSession</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputerName&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用凭证进入远程会话</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$cred </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Credential</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Enter-PSSession</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ComputerName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;RemoteComputerName&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Credential $cred</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 退出远程会话</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Exit-PSSession</span></span></code></pre></figure>
<h3 id="高级网络配置">高级网络配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高级网络配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="配置网络流量控制qos">配置网络流量控制QoS<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置网络流量控制qos" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建 QoS 策略</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetQosPolicy</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;QoSPolicy1&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AppPathNameMatchCondition </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;C:\Program Files\ExampleApp\Example.exe&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">IPDstPortMatchCondition </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ThrottleRateActionBitsPerSecond </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">50000000</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看现有 QoS 策略</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetQosPolicy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除 QoS 策略</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-NetQosPolicy</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;QoSPolicy1&quot;</span></span></code></pre></figure>
<h4 id="配置路由">配置路由<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置路由" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 查看路由表</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-NetRoute</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 添加静态路由</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">New-NetRoute</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DestinationPrefix </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;10.0.0.0/24&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">NextHop </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;192.168.1.1&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除静态路由</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Remove-NetRoute</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DestinationPrefix </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;10.0.0.0/24&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">NextHop </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;192.168.1.1&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">InterfaceAlias </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Ethernet&quot;</span></span></code></pre></figure>
<p>通过以上内容和示例,</p>
<h2 id="自动化">自动化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 是一个功能强大的自动化工具,可以帮助系统管理员和 IT 专业人员简化和自动化各种任务。本文将介绍如何使用 PowerShell 创建和管理计划任务,以及自动化日常管理任务的示例。</p>
<h3 id="任务计划">任务计划<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#任务计划" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="使用-powershell-创建计划任务">使用 PowerShell 创建计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-powershell-创建计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PowerShell 提供了 <code>ScheduledTasks</code> 模块,可以方便地创建和管理计划任务。以下是一些常见的计划任务操作示例:</p>
<h5 id="创建计划任务">创建计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>New-ScheduledTaskAction</code><code>New-ScheduledTaskTrigger</code><code>Register-ScheduledTask</code> 命令可以创建计划任务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义任务动作</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskAction</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Execute </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'PowerShell.exe'</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Argument </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-NoProfile -WindowStyle Hidden -File C:\Scripts\MyScript.ps1'</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义触发器,每天上午 8 点执行</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskTrigger</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Daily </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">At 8am</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义任务设置</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$settings </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskSettingsSet</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AllowStartIfOnBatteries </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DontStopIfGoingOnBatteries</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Register-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action $action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Trigger $trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Settings $settings </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Description </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;My daily PowerShell script task&quot;</span></span></code></pre></figure>
<h5 id="查看计划任务">查看计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#查看计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Get-ScheduledTask</code> 命令可以查看所有或特定的计划任务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ScheduledTask</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取特定计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span></span></code></pre></figure>
<h5 id="修改计划任务">修改计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#修改计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Set-ScheduledTask</code> 命令可以修改现有计划任务的触发器、动作等。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 修改计划任务的触发器</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskTrigger</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Weekly </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DaysOfWeek Monday</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Wednesday</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Friday </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">At 8am</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Trigger $trigger</span></span></code></pre></figure>
<h5 id="删除计划任务">删除计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#删除计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Unregister-ScheduledTask</code> 命令可以删除计划任务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Unregister-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Confirm:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$false</span></span></code></pre></figure>
<h4 id="任务的调度和管理">任务的调度和管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#任务的调度和管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>除了创建计划任务PowerShell 还可以管理任务的调度和执行情况。</p>
<h5 id="启动和停止计划任务">启动和停止计划任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动和停止计划任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Start-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停止计划任务</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Stop-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span></span></code></pre></figure>
<h5 id="获取计划任务运行历史">获取计划任务运行历史<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取计划任务运行历史" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用 <code>Get-ScheduledTaskInfo</code> 命令可以获取计划任务的运行历史。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取任务的最近运行历史</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ScheduledTaskInfo</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;MyDailyTask&quot;</span></span></code></pre></figure>
<h3 id="自动化常见任务">自动化常见任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动化常见任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="自动化日常管理任务">自动化日常管理任务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动化日常管理任务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>PowerShell 可以自动化许多日常管理任务,如备份文件、清理日志、监控系统等。</p>
<h5 id="自动备份文件">自动备份文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动备份文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>以下脚本展示了如何使用 PowerShell 自动备份文件。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 自动备份文件脚本</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$sourcePath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\Data&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$backupPath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;D:\Backup&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$timestamp </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Date</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Format </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;yyyyMMddHHmmss&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$backupDestination </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$backupPath</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">\DataBackup_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$timestamp</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.zip&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 压缩并备份文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Compress-Archive</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $sourcePath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">DestinationPath $backupDestination</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Backup completed: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$backupDestination</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span></code></pre></figure>
<h5 id="自动清理日志">自动清理日志<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动清理日志" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>以下脚本展示了如何使用 PowerShell 自动清理超过 30 天的日志文件。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 自动清理超过 30 天的日志文件</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$logPath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\Logs&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$daysOld </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 30</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取并删除旧日志文件</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-ChildItem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Path $logPath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Filter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.log </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Where-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.LastWriteTime </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Date</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).AddDays(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$daysOld) } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Remove-Item</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Force</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Old log files cleaned up.&quot;</span></span></code></pre></figure>
<h4 id="脚本的自动化执行">脚本的自动化执行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#脚本的自动化执行" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过计划任务或事件触发器,可以自动执行 PowerShell 脚本,实现任务自动化。</p>
<h5 id="通过计划任务自动执行脚本">通过计划任务自动执行脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#通过计划任务自动执行脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用计划任务可以定期执行脚本。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建计划任务以定期执行脚本</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskAction</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Execute </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'PowerShell.exe'</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Argument </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-NoProfile -WindowStyle Hidden -File C:\Scripts\MyScript.ps1'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskTrigger</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Daily </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">At 6pm</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Register-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action $action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Trigger $trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;EveningScriptTask&quot;</span></span></code></pre></figure>
<h5 id="事件触发自动执行脚本">事件触发自动执行脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#事件触发自动执行脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>使用事件触发器可以在特定事件发生时执行脚本,例如系统启动时。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建事件触发器</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskTrigger</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">AtStartup</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建计划任务在系统启动时执行脚本</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> New-ScheduledTaskAction</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Execute </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'PowerShell.exe'</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Argument </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-NoProfile -WindowStyle Hidden -File C:\Scripts\StartupScript.ps1'</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Register-ScheduledTask</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Action $action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Trigger $trigger </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">TaskName </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;StartupScriptTask&quot;</span></span></code></pre></figure></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#文件系统管理" data-for="文件系统管理">文件系统管理</a></li><li class="depth-1"><a href="#管理文件和文件夹" data-for="管理文件和文件夹">管理文件和文件夹</a></li><li class="depth-1"><a href="#文件内容的读取和写入" data-for="文件内容的读取和写入">文件内容的读取和写入</a></li><li class="depth-1"><a href="#搜索文件和文件夹" data-for="搜索文件和文件夹">搜索文件和文件夹</a></li><li class="depth-1"><a href="#文件属性和权限管理" data-for="文件属性和权限管理">文件属性和权限管理</a></li><li class="depth-1"><a href="#压缩和解压文件" data-for="压缩和解压文件">压缩和解压文件</a></li><li class="depth-1"><a href="#管理临时文件和文件夹" data-for="管理临时文件和文件夹">管理临时文件和文件夹</a></li><li class="depth-0"><a href="#注册表管理" data-for="注册表管理">注册表管理</a></li><li class="depth-1"><a href="#注册表概述" data-for="注册表概述">注册表概述</a></li><li class="depth-1"><a href="#读取和修改注册表项" data-for="读取和修改注册表项">读取和修改注册表项</a></li><li class="depth-1"><a href="#管理注册表权限" data-for="管理注册表权限">管理注册表权限</a></li><li class="depth-1"><a href="#备份和还原注册表项" data-for="备份和还原注册表项">备份和还原注册表项</a></li><li class="depth-0"><a href="#进程和服务管理" data-for="进程和服务管理">进程和服务管理</a></li><li class="depth-1"><a href="#管理系统进程" data-for="管理系统进程">管理系统进程</a></li><li class="depth-1"><a href="#管理-windows-服务" data-for="管理-windows-服务">管理 Windows 服务</a></li><li class="depth-1"><a href="#windows-服务管理" data-for="windows-服务管理">Windows 服务管理</a></li><li class="depth-1"><a href="#配置和管理-windows-服务" data-for="配置和管理-windows-服务">配置和管理 Windows 服务</a></li><li class="depth-1"><a href="#示例脚本" data-for="示例脚本">示例脚本</a></li><li class="depth-0"><a href="#用户和权限管理" data-for="用户和权限管理">用户和权限管理</a></li><li class="depth-1"><a href="#用户和组的管理" data-for="用户和组的管理">用户和组的管理</a></li><li class="depth-1"><a href="#权限设置" data-for="权限设置">权限设置</a></li><li class="depth-1"><a href="#远程用户和权限管理" data-for="远程用户和权限管理">远程用户和权限管理</a></li><li class="depth-0"><a href="#网络管理" data-for="网络管理">网络管理</a></li><li class="depth-1"><a href="#网络配置" data-for="网络配置">网络配置</a></li><li class="depth-1"><a href="#测试网络连接" data-for="测试网络连接">测试网络连接</a></li><li class="depth-1"><a href="#远程管理" data-for="远程管理">远程管理</a></li><li class="depth-1"><a href="#高级网络配置" data-for="高级网络配置">高级网络配置</a></li><li class="dep
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>