I can't figure out why the following code fails:
# test.ps1
"`$args: ($args)"
"`$args count: $($args.length)"
# this fails
0..$($args.length - 1) | %{ $args[$_] = ($args[$_] -replace '`n',"`n") }
# this works
$i = 0
foreach ( $el in $args ) { $args[$i] = $args[$i] -replace '`n',"`n"; $i++ }
"$args"
I'm calling it like so:
rem from ...
Take the Winlogon registry section, I would like PowerShell to display the Data value for
DefaultUserName.
This is as far as I have got:
Stage 1
get-itemproperty -path "hklm:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\"
Stage 2
I can append:
-Name DefaultUserName
But this won't return a value.
Also other Names, despite...
When I run the below code on first attempt I get an unexplained error, but running the script again on a second attempt works fine...what would be wrong in my code?
By the way I am creating the database before this step...
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dBS...
I am a powershell novice.
After days of searching....
I have put together a small powershell script (as below) to check page file, /PAE switch, /3GB switch, SQL server max RAM, min RAM.
I am running this on 1 server.
If I want to run it on many servers (from a .txt) file, How can I change it ?
How can I change it to search boot.ini fi...
My code below works perfectly on a 32-bit Windows machine, however it refuses to work on a 64-bit machine due to the 32-bit WMI class win32reg_addremoveprograms used in the code. Is there a 64-bit equivalent of this class?
$ServerFile = "D:\SharePoint\Powershell\AddRemovePrograms\Machines.txt"
$ServerList = Get-Content $ServerFile
$Exc...
How to read the below xml file and write its content into a plain text file?
<!--
< config >
< compare >
< source >d:\demo\< /source >
< destination >e:\demo\< / destination >
< parameters >
< parameter >FileName< /parameter>
< parameter >Size< /parameter>
< parameter>Date< /parameter>
< /parameters >
< /compare >
< ...
I am trying to connect to a remote sql database and simply run the "select @@servername" query in Powershell. I'm trying to do this without using integrated security. I've been struggling with "get-credential" and "invoke-sqlcmd", only to find (I think), that you can't pass the password from "get-credential" to another Powershell cmdle...
I'm trying to programmatically create a parameter block for a function ( along the lines of this blog post ).
I'm starting with a CommandMetadata object (from an existing function). I can create the ParameterMetadata object and set things like the ParameterType, the name, as well as some attributes.
The problem I'm running into is t...
I've created a set of virtual machines (Windows Server) with a specific admin password; these VMs have been assigned to users, and may be in use. I want to know if the user changed the admin password, and do the check so the user doesn't notice. What are good solutions in powershell?
...
The tabexpansion function only works partially when I override it like so:
function tabexpansion {
param($line, $lastWord)
if ($line -eq "hey ") {
"you", "Joe"
}
}
The custom completions work as expected, but now I only get the default autocomplete behavior for cmdlet names, not parameters. So New-TAB works fine, ...
I may be trying an invalid approach, so I'm open to any suggestions.
I'm running a series of 3 scripts that each do an analysis of websites on an IIS server, and I'm running them against a couple hundred servers. I'm proof-of-concepting doing this as a Start-Job process so I can run in parallel and finish a lot more quickly. These scri...
$source |% {
switch -regex ($_){
'\<'+$primaryKey+'\>(.+)\</'+$primaryKey+'\>' {
$primaryKeyValue = $matches[1]; continue; }
}
I want to use dynamic key value with switch-regex, is that possible?
...
How to find the depth of the xml file using powershell/xpath?
consider the below xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title>Harry Potter</title>
<price>25.99</price>
</book>
<book>
<title>Learning XML</title>
<price>49.95</price>
</book>
</bookstore>
depth of the above xml document is 3 (bookst...
Can I use a variable in and LdapFilter with Get-QADComputer? i.e.:
$31DaysAgo = (Get-Date).AddDays(-31)
$ft = $31DaysAgo.ToFileTime()
$StComps = Get-QADComputer -SizeLimit 0 -IncludeAllProperties -SearchRoot `
'DC=MY,DC=DOMAIN,DC=LOCAL' -LdapFilter '(&(objectcategory=computer) `
(pwdLastSet<=$ft)(|(operatingsystem=Windows 2000 Profess...
I'm starting to create a PowerShell Provider. To start off, I have a class inheriting from NavigationCmdletProvider.
There's only one protected constructor and it requires an instance of ProviderInfo. Yes, that's ProviderInfo .ctor requiring instance of ProviderInfo.
In creating my first test around this provider, I can't seem to find ...
I need to copy files in one directory to another directory where the lastwritetime is greater than or equal to 3/26/2010 9:00pm. I'm using:
Get-ChildItem C:\pstest\hlstore\folder1\data | where-object {$i.lastwritetime -ge “3/26/2010 9:00 PM”} | Copy-Item -destination c:\pstest\hlstore2\folder1\data
But nothing happens...
Any help wo...
I'm looking to run some powershell scripts via automation. Something like:
IList errors;
Collection<PSObject> res = null;
using (RunspaceInvoke rsi = new RunspaceInvoke())
{
try
{
res = rsi.Invoke(commandline, null, out errors);
}
catch (Exception ex)
{
LastErrorMessage = ex.ToString();
Debug....
I'm working on a PoSh project that generates CSharp code, and then Add-Types it into memory.
The new types use existing types in an on disk DLL, which is loaded via Add-Type.
All is well and good untill I actualy try to invoke methods on the new types. Here's an example of what I'm doing:
$PWD = "."
rm -Force $PWD\TestClassOne*
$code...
What are some of the best practices for writting scripts that will execute in a remote context?
For instance, I just discovered that built-in var $Profile doesn't exist during remote execution.
...
Has anyone managed to use this module with success, i'm running 32bit Windows 7, where i have opened an administrator shell using run as administrator, i have imported the WebAdministration module and then attempted to use the commands with some issues, have provided two examples here
Websites
I created a web site with the following co...