请考虑以下PowerShell代码段:
$csharpString = @"
using System;
public sealed class MyClass
{
public MyClass() { }
public override string ToString() {
return "This is my class. There are many others " +
"like it, but this one is mine.";
}
}
"@
Add-Type -TypeDefinition $csharpString;
$myObject = New-Object MyClass
Write-Host $myObject.ToString();
Run Code Online (Sandbox Code Playgroud)
如果我在同一个AppDomain中多次运行它(例如在powershell.exe或powershell_ise.exe中运行两次脚本),我会收到以下错误:
Add-Type : Cannot add type. The type name 'MyClass' already exists.
At line:13 char:1
+ Add-Type -TypeDefinition $csharpString;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (MyClass:String) [Add-Type],
Exception
+ FullyQualifiedErrorId …Run Code Online (Sandbox Code Playgroud) 如何检查Powershell以查看$ fullPath中的文件是否超过"5天10小时5分钟"?
(OLD,我的意思是如果它是在5天10小时5分钟之前创建或修改的)
我有一个包含几千个文件夹的基目录.在这些文件夹中,可以有1到20个子文件夹,其中包含1到10个文件.我想删除所有超过60天的文件.我使用下面的代码来获取我必须删除的文件列表:
DirectoryInfo dirInfo = new DirectoryInfo(myBaseDirectory);
FileInfo[] oldFiles =
dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
.Where(t=>t.CreationTime < DateTime.Now.AddDays(-60)).ToArray();
Run Code Online (Sandbox Code Playgroud)
但我让它运行了大约30分钟,但仍然没有完成.我很好奇是否有人能够看到我可以提高上述线路的性能,或者如果有不同的方式我应该完全接近这个以获得更好的性能?建议?
我在Linux上忘记了我的gpg密钥的密码.有人可以帮我写一个简单的脚本来使用暴力破解密钥吗?我记得密码中可能有的一些词,所以希望我的计算机不会花费很长时间来强制它.
如果我无法恢复密码,一切都不会丢失,这只是意味着我将无法在接下来的10天内处理我的项目,直到我重新开始工作以获取另一个文件副本,但这次是我将记住密码的新密钥.
但是,能够在这10天内完成我的项目是很好的.
public delegate void SecondChangedHandler(
object clock,
TimeInfoEventArgs timeInformation);
public event SecondChangedHandler SecondChanged;
Run Code Online (Sandbox Code Playgroud)
我根据这篇文章写了一个时钟.现在,如果我删除事件关键字,我得到相同的结果,那么事件真正做了什么?
我正在尝试创建一个能够运行各种Powershell脚本的Windows应用程序.
我有一个脚本,它应该工作(从Powershell提示符运行时),我的Windows应用程序似乎执行它应该,但它无法在我的OU上找到方法.
当我从Windows应用程序执行脚本时,我收到这些消息:
错误:检索成员"创建"时发生以下异常:"服务器上没有此类对象."
错误:检索成员"删除"时发生以下异常:"服务器上没有此类对象."
Powershell脚本:
function New-AdUser {
param (
[string] $Username = $(throw "Parameter -Username [System.String] is required."),
[string] $Password = $(throw "Parameter -Password [System.String] is required."),
[string] $OrganizationalUnit = "Users",
[string] $DisplayName,
[string] $FirstName,
[string] $LastName,
[string] $Initials,
[string] $MobilePhone,
[string] $Description,
[switch] $CannotChangePassword,
[switch] $PasswordNeverExpires,
[switch] $Disabled
)
try {
$currentDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$dn = $currentDomain.GetDirectoryEntry().distinguishedName
$ou = [ADSI] "LDAP://CN=$OrganizationalUnit,$dn"
$userAccount = $ou.Create("user", "cn=$Username")
$userAccount.SetInfo()
$userAccount.userAccountControl = ($userAccount.userAccountControl.Item(0) -bxor 0x0002) #Enable the account
$userAccount.SetInfo()
$userAccount.sAMAccountName = …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的C#代码运行PowerShell脚本,它将使用运行它们的程序集中的自定义Cmdlet.这是代码:
using System;
using System.Management.Automation;
[Cmdlet(VerbsCommon.Get,"Hello")]
public class GetHelloCommand:Cmdlet
{
protected override void EndProcessing()
{
WriteObject("Hello",true);
}
}
class MainClass
{
public static void Main(string[] args)
{
PowerShell powerShell=PowerShell.Create();
powerShell.AddCommand("Get-Hello");
foreach(string str in powerShell.AddCommand("Out-String").Invoke<string>())
Console.WriteLine(str);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到一个CommandNotFoundException.我写错了Cmdlet吗?有什么我需要做的事情来在PowerShell或Runspace中注册我的Cmdlet吗?
我试图在不使用foreach语句的情况下访问ManagementObjectCollection中的ManagementObjects,也许我错过了一些但我无法弄清楚如何去做,我需要做类似以下的事情:
ManagementObjectSearcher query = new ManagementObjectSearcher(
"select Name, CurrentClockSpeed from Win32_Processor");
ManagementObjectCollection queryCollection = query.Get();
ManagementObject mo = queryCollection[0];
Run Code Online (Sandbox Code Playgroud) 我最近发现Powershell函数只是命名为scriptblocks.例如
function HelloWorld {
Write-Output "Hello world"
}
$hw = $function:HelloWorld
& $hw
Run Code Online (Sandbox Code Playgroud)
将执行HelloWorld方法.
但是,我无法弄清楚的是,如何获得对其名称中包含破折号的方法的引用:
function Hello-World {
Write-Output "Hello world"
}
$hw = $function:Hello-World
You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:27
+ $hw = $function:Hello- <<<< World
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我知道我可以这样做:
$hw = (Get-Item function:Hello-World).ScriptBlock
Run Code Online (Sandbox Code Playgroud)
但它有点"嘈杂",我喜欢$ function语法
处理函数内部创建的对象的正确方法是什么?我在网站上遇到过这种方法.
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
$site = get-SPSite $weburl
return $site.OpenWeb()
$site.Dispose()
}
Run Code Online (Sandbox Code Playgroud)
是否在此函数中调用Dispose方法?
powershell ×6
c# ×5
.net ×2
add-type ×1
bash ×1
brute-force ×1
cmdlet ×1
events ×1
function ×1
gnupg ×1
linux ×1
performance ×1
sharepoint ×1
ubuntu ×1
wmi ×1