我一直致力于在 Powershell 中创建一个基本的 Conway's Game of Life 模拟器,以更加熟悉该语言。但是我当前的代码错误地计算了相邻单元格的数量,因此游戏无法运行。我相信我的包装是正确的,据我所知,但我的邻居数量似乎“上升”了。
这是代码:
function next-gen{
param([System.Array]$origM)
$tmpM = $origM
For($x=0; $x -lt $tmpM.GetUpperBound(0); $x++ ){
For($y=0; $y -lt $tmpM.GetUpperBound(1); $y++){
$neighborCount = getNeighbors $tmpM $x $y
if($neighborCount -lt 2 -OR $neighborCount -gt 3){
$tmpM[$x,$y] = 0
}
elseif($neighborCount -eq 3){
$tmpM[$x, $y] = 1
}
}
}
$Global:origM = $tmpM
}
function getNeighbors{
param(
[System.Array]$g,
[Int]$x,
[Int]$y
)
$newX=0
$newY=0
$count=0
for($newX = -1; $newX -le 1; $newX++){
for($newY = -1; $newY -le 1; …
Run Code Online (Sandbox Code Playgroud) 我一直在比较在Powershell中快速读取相对较大的文本文档的各种方法。这些文件的大小范围为 50kb - 200mb。我需要快速解析它们以获取特定的行和/或特定的字符串。
读取文件的三个常用工具(我知道,并且没有构建我自己的 C# 库)是:System.IO.StreamReader、System.IO.File 和 Powershell Cmdlet Get-Content。
所以我写了一个快速的小比较脚本:
$file = Get-Childitem -path "MyLogFile.txt" #This is a 100mb txt file
$t1 = Measure-Command{
$reader = New-Object System.IO.StreamReader($file)
$content = $reader.ReadToEnd()
$reader.Close()
}
Write-host "StreamReader time: " + $t1
$t2 = Measure-Command{
Get-Content $file
}
Write-host "Get-Content time: " + $t2
$t3 = Measure-Command {
$reader = [System.IO.File]::OpenText($file)
$content = $reader.ReadToEnd()
$reader.Close()
}
Write-Host "System.IO.File reader time: " + $t3
Run Code Online (Sandbox Code Playgroud)
它产生(当然略有变化)以下输出:
StreamReader time: + 00:00:00.5493247
Get-Content time: + …
Run Code Online (Sandbox Code Playgroud) 很难在我的网站上验证reCaptcha :(
我曾尝试为VB.net实现找到其他来源,但运气不佳。这是我尝试过的...
default.aspx.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.Script.Serialization
Public Class _Default
Inherits System.Web.UI.Page
Sub reCaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
If (capValidate()) Then
MsgBox("Valid Recaptcha")
Else
MsgBox("Not Valid Recaptcha")
End If
End Sub
Public Function capValidate() As Boolean
Dim Response As String = Request("g-captcha-response")
Dim Valid As Boolean = False
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.google.com/recaptcha/api/siteverify?secret=THIS IS WHERE MY KEY IS&response=") & …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 powershell 中的作业启动/停止服务。在我的本地机器上进行测试时,作业将进入完成状态,但服务不会更改其状态。有没有办法更好地了解我的工作做了什么?
这是代码:
$service = 'MSSQL$SQLEXPRESS'
$server = 'myPC'
$myJob = Start-Job -Scriptblock {Get-Service -Name $args[0] -computername $args[1] | Set-Service -Status Running} -ArgumentList @($service,$server)
sleep -Seconds 10
get-job
Run Code Online (Sandbox Code Playgroud)
当我运行该代码段时,SQL SQL (EXPRESS) 服务没有启动。
但是,如果我从提升的 shell 运行以下命令,它将启动:
Get-Service -Name $service -computername $server | Set-Service -Status Running
Run Code Online (Sandbox Code Playgroud)
当我运行 Get-Job 时,这是我看到的......
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
10 Job10 BackgroundJob Completed True localhost Get-Service -Name $arg...
Run Code Online (Sandbox Code Playgroud)
它们都从提升的 shell 运行,所以我绝对有权启动/停止服务。