{"id":55776,"date":"2023-06-26T17:39:15","date_gmt":"2023-06-26T09:39:15","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=55776"},"modified":"2023-06-26T17:39:15","modified_gmt":"2023-06-26T09:39:15","slug":"microsoft-powershell-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/microsoft-powershell-linux.html","title":{"rendered":"PowerShell\u6765\u641e\u4e8b\u60c5\u5566\uff01"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u5fae\u8f6f\u7231\u4e0aLiunx\u4e4b\u540e\uff0cPowerShell<\/strong> \u8fd9\u4e2a\u539f\u672c\u53ea\u662f Windows \u624d\u80fd\u4f7f\u7528\u7684\u7ec4\u4ef6\uff0c\u4e8e 2016 \u5e74 8 \u6708 18 \u65e5\u5f00\u6e90\u5e76\u4e14\u6210\u4e3a\u8de8\u5e73\u53f0\u8f6f\u4ef6\uff0c\u767b\u9646\u4e86 Linux \u548c macOS\u3002\u672c\u6587\u7ae0\u4e2d\u6211\u4eec\u4e3b\u8981\u5c55\u793a\u4e86\u5982\u4f55\u5728 Linux \u4e2d\u5b89\u88c5\u5fae\u8f6f\u7684 PowerShell Core 6.0<\/strong>\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

PowerShell<\/strong> \u662f\u4e00\u4e2a\u5fae\u8f6f\u5f00\u53d1\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u548c\u914d\u7f6e\u7ba1\u7406\u7cfb\u7edf\u3002\u5b83\u57fa\u4e8e .NET \u6846\u67b6\uff0c\u7531\u547d\u4ee4\u884c\u8bed\u8a00\u89e3\u91ca\u5668\uff08shell\uff09\u548c\u811a\u672c\u8bed\u8a00\u7ec4\u6210\u3002<\/p>\n

PowerShell \u63d0\u4f9b\u5bf9 COM\uff08\u7ec4\u4ef6\u5bf9\u8c61\u6a21\u578b\uff09<\/strong>\u00a0\u548c WMI\uff08Windows \u7ba1\u7406\u89c4\u8303\uff09<\/strong>\u7684\u5b8c\u5168\u8bbf\u95ee\uff0c\u4ece\u800c\u5141\u8bb8\u7cfb\u7edf\u7ba1\u7406\u5458\u5728\u672c\u5730\u6216\u8fdc\u7a0b Windows \u7cfb\u7edf\u4e2d \u6267\u884c\u7ba1\u7406\u4efb\u52a1\uff0c\u4ee5\u53ca\u5bf9 WS-Management \u548c CIM\uff08\u516c\u5171\u4fe1\u606f\u6a21\u578b\uff09\u7684\u8bbf\u95ee\uff0c\u5b9e\u73b0\u5bf9\u8fdc\u7a0b Linux \u7cfb\u7edf\u548c\u7f51\u7edc\u8bbe\u5907\u7684\u7ba1\u7406\u3002<\/p>\n

\u901a\u8fc7\u8fd9\u4e2a\u6846\u67b6\uff0c\u7ba1\u7406\u4efb\u52a1\u57fa\u672c\u4e0a\u7531\u79f0\u4e3a cmdlets<\/strong>\u7684 .NET<\/strong> \u7c7b\u6267\u884c\u3002\u5c31\u50cf Linux \u7684 shell \u811a\u672c\u4e00\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\u5c06\u4e00\u7ec4 cmdlets<\/strong> \u5199\u5165\u6587\u4ef6\u6765\u5236\u4f5c\u811a\u672c\u6216\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e9b\u811a\u672c\u53ef\u4ee5\u7528\u4f5c\u72ec\u7acb\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u6216\u8005\u5de5\u5177\u3002<\/p>\n

\u00a0\u5728Linux\u7cfb\u7edf\u4e2d\u5b89\u88c5PowerShell \u00a0Core 6.0<\/strong><\/div>\n

\u8981\u5728 Linux \u4e2d\u5b89\u88c5 PowerShell Core 6.0<\/strong>\uff0c\u6211\u4eec\u5c06\u4f1a\u7528\u5230\u5fae\u8f6f\u8f6f\u4ef6\u4ed3\u5e93\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6700\u6d41\u884c\u7684 Linux \u5305\u7ba1\u7406\u5668\u5de5\u5177\uff0c\u5982apt-get\u3001yum\u7b49\u6765\u5b89\u88c5\u3002<\/p>\n

\u5728 Ubuntu 16.04 \u4e2d\u5b89\u88c5<\/strong><\/span><\/div>\n

\u9996\u5148\uff0c\u5bfc\u5165\u8be5\u516c\u5171\u4ed3\u5e93\u7684 GPG<\/strong> \u5bc6\u94a5\uff0c\u7136\u540e\u5c06 Microsoft Ubuntu<\/strong> \u4ed3\u5e93\u6ce8\u518c\u5230 APT<\/strong> \u7684\u6e90\u4e2d\u6765\u5b89\u88c5 PowerShell<\/strong>\uff1a<\/p>\n

$ curl https:\/\/packages.microsoft.com\/keys\/microsoft.asc | sudo apt-key add -\r\n$ curl https:\/\/packages.microsoft.com\/config\/ubuntu\/16.04\/prod.list | sudo tee \/etc\/apt\/sources.list.d\/microsoft.list\r\n$ sudo apt-get update\r\n$ sudo apt-get install -y powershell\r\n<\/pre>\n
\u5728 Ubuntu 14.04 \u4e2d\u5b89\u88dd<\/strong><\/span><\/div>\n
$ curl https:\/\/packages.microsoft.com\/keys\/microsoft.asc | sudo apt-key add -\r\n$ curl https:\/\/packages.microsoft.com\/config\/ubuntu\/14.04\/prod.list | sudo tee \/etc\/apt\/sources.list.d\/microsoft.list\r\n$ sudo apt-get update\r\n$ sudo apt-get install -y powershell<\/pre>\n
\u5728 CentOS 7 \u4e2d\u5b89\u88dd<\/strong><\/span><\/div>\n

\u9996\u5148\uff0c\u5c06 Microsoft RedHat<\/strong> \u4ed3\u5e93\u6ce8\u518c\u5230 YUM<\/strong> \u5305\u7ba1\u7406\u5668\u4ed3\u5e93\u5217\u8868\u4e2d\uff0c\u7136\u540e\u5b89\u88c5 PowerShell<\/strong>\uff1a<\/p>\n

$ sudo curl https:\/\/packages.microsoft.com\/config\/rhel\/7\/prod.repo > \/etc\/yum.repos.d\/microsoft.repo\r\n$ sudo yum install -y powershell<\/pre>\n
\n

\u5982\u4f55\u5728 Linux \u4e2d\u4f7f\u7528 PowerShell Core 6.0<\/strong><\/h3>\n<\/div>\n

\u5728\u8fd9\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u7b80\u5355\u4ecb\u7ecd\u4e0b PowerShell<\/strong>\uff1b\u6211\u4eec\u5c06\u4f1a\u770b\u5230\u5982\u4f55\u542f\u52a8 PowerShell\uff0c\u8fd0\u884c\u4e00\u4e9b\u57fa\u7840\u547d\u4ee4\uff0c\u64cd\u4f5c\u6587\u4ef6\u3001\u76ee\u5f55\u548c\u8fdb\u7a0b\u3002\u7136\u540e\u5b66\u4e60\u600e\u6837\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u547d\u4ee4\u3001\u663e\u793a\u547d\u4ee4\u5e2e\u52a9\u548c\u522b\u540d\u3002<\/p>\n

\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8 PowerShell\uff1a<\/p>\n

$PSVersionTable<\/pre>\n

\"\"
\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u67e5\u770b PowerShell \u7248\u672c\uff1a<\/p>\n

$ powershell<\/pre>\n

\"\"
\n\u5728 Linux \u4e2d\u8fd0\u884c\u57fa\u672c\u7684 PowerShell \u547d\u4ee4:<\/p>\n

get-date          [# \u663e\u793a\u5f53\u524d\u65e5\u671f]\r\nget-uptime        [# \u663e\u793a\u5f00\u673a\u65f6\u95f4]\r\nget-location      [# \u663e\u793a\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55]<\/pre>\n

\"\"<\/p>\n

\u5728 PowerShell \u4e2d\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/span><\/div>\n

1\u3001\u00a0\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u521b\u5efa\u7a7a\u6587\u4ef6\uff1a<\/p>\n

new-item  tecmint.tex\r\n\u6216\u8005\r\n\"\">tecmint.tex<\/pre>\n

\u7136\u540e\u5f80\u91cc\u9762\u6dfb\u52a0\u5185\u5bb9\u5e76\u67e5\u770b\u6587\u4ef6\u5185\u5bb9:<\/p>\n

set-content tecmint.tex -value \"TecMint Linux How Tos Guides\"\r\nget-content tecmint.tex<\/pre>\n

2\u3001\u00a0\u5728 PowerShell \u4e2d\u5220\u9664\u4e00\u4e2a\u6587\u4ef6<\/p>\n

remove-item tecmint.tex\r\nget-content tecmint.tex<\/pre>\n

3\u3001\u00a0\u521b\u5efa\u76ee\u5f55:<\/p>\n

mkdir  tecmint-files\r\ncd  tecmint-files\r\n\u201c\u201d>domains.list\r\nls<\/pre>\n

\"\"<\/p>\n

4\u3001\u00a0\u6267\u884c\u957f\u683c\u5f0f\u7684\u5217\u8868\u64cd\u4f5c\uff0c\u5217\u51fa\u6587\u4ef6\uff0f\u76ee\u5f55\u8be6\u7ec6\u60c5\u51b5\uff0c\u5305\u62ec\u6a21\u5f0f\uff08\u6587\u4ef6\u7c7b\u578b\uff09\u3001\u6700\u540e\u4fee\u6539\u65f6\u95f4\u7b49\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

dir<\/pre>\n

\"\"<\/p>\n

5\u3001\u00a0\u663e\u793a\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8fdb\u7a0b\uff1a<\/p>\n

get-process<\/pre>\n

\"\"<\/p>\n

6\u3001\u00a0\u901a\u8fc7\u7ed9\u5b9a\u7684\u540d\u79f0\u67e5\u770b\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\/\u8fdb\u7a0b\u7ec4\u7ec6\u8282\uff0c\u5c06\u8fdb\u7a0b\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u4e0a\u9762\u7684\u547d\u4ee4\uff0c\u5982\u4e0b\uff1a<\/p>\n

get-process apache2<\/pre>\n

\"\"<\/p>\n

\u8f93\u51fa\u4e2d\u5404\u90e8\u5206\u7684\u542b\u4e49\uff1a<\/p>\n