Quantcast
Channel: Windows PowerShell Forum
Viewing all 2562 articles
Browse latest View live

PowerShell 3.0 - Invoke-WebRequest -Method parameter

$
0
0

Cannot bind parameter 'Method'. Cannot convert value "SEARCH" to type "Microsoft.PowerShell.Commands.WebRequestMethod". Error: "Unable to match the identifier name SEARCH to a valid enumerator name.  Specify one of the following enumerator names and try again: Default, Get, Head, Post, Put, Delete, Trace, Options, Merge, Patch"

It is really inappropriate to represent HTTP method as a constant value. 


Trying to configure Pop-up message to display correctly

$
0
0

Hello!

I have a small script that will show a pop-up message when I call upon the Function for it. Here is the Function:

Function Tester (){
$dataExtractedFolder = (Get-Location).Path
$result = [Microsoft.VisualBasic.Interaction]::MsgBox("The Report has been placed in $dataExtractedFolder",'OKOnly,SystemModal,Information', 'Report Generated!')
$result
}
Tester

When I insert this into the shell, it works fine. It will display the correct message like so:

But when I save the function into a file like test.ps1 and try to execute by right-clicking and "Run with PowerShell", it shows me an error message that flashes this then goes away:

Any suggestions on how to make message display while right clicking on the test.ps1 file?

Actually this is apart of problem that I am having when inserting the function into a bigger function that contains several functions. The message does not display at the end so I am drawing a conclusion to say that the reason why is above.

Thanks in advanced!

Get Registry subkeys with special chars

$
0
0

I need to find and identify all lan and wlan adapters on a Win7 PC.

I tried following but it fails I guess because the {} chars in registry key and other faults.

$x = Get-ChildItem -Path 'HKLM:SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}' -Exclude "Descriptions" | ForEach-Object {$_.Name.OpenSubKey("Connection") | Where (Get-ItemProperty ($_.PnPInstanceID).Substring(0,3) -eq "PCI" -and (Get-ItemProperty $_.MediaSubType -eq 2)) | Get-ItemProperty $_.Name }

thanks for any help

Importing CSV and filtering it via TXT file

$
0
0

Hello everyone. Please pardon me if I don't make too much sense with what I'm trying to accomplish. We have a database that extracts data into a CSV file nightly. This database contains a lot of information concerning our physical servers. Whenever we decommission servers we have to fill spreadsheets with information gathered from this CSV file. However, I'm trying to only filter out certain columns and servers (the ones being decommissioned) from the CSV file. 

Here is what I have so far:

$test = Import-CSV ".\aperture_servers.csv" |
Select @{Name="Server Name";Expression={$_."device_name"}},
        @{Name="IP Address";Expression={$_."nic_1_text"}},
        @{Name="ILO IP";Expression={$_."console_ilo_text"}},
        @{Name="Building";Expression={$_."building"}},
        @{Name="Floor";Expression={$_."floor"}},
        @{Name="Grid";Expression={$_."placement_grid"}},
        @{Name="Serial #";Expression={$_."serial_number"}}
        
$test | export-csv new-test.csv –notype

This works fine, however, it extracts EVERY (thousands) of servers into the new-test.csv file. I want to create a servers.txt file that has a list of servers and this way the export-csv command only exports servers that match the servers.txt file. Any help would be appreciated. (Sorry I'm very newb at this stuff).

user logon logon off data recording script.. I want to know how many days I can create user logon log off events from following script.. I need to record user logon and logoff events for month of period

$
0
0

script im using to recording user logon and log off events is below mentioned. my VB script knowledge is not that high. can anybody explain for how many days I can record user logon logoff events ? i need to record it for a month of period

WriteLineToFile()


Function WriteLineToFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set THENetwork = WScript.CreateObject("WScript.Network")

  filename = "\\10.10.10.3\sysvol\MEDIGAINSL.COM\Log\" & THENetwork.UserName & ".csv"
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set fso = f.OpenTextFile(filename, ForAppending, True)
  qMark = Unescape("%22")
  coma = Unescape("%2C")
  today = date()
  curTime = Time
  username = THENetwork.UserName
  computername = THENetwork.ComputerName
  f.Writeline()
  f.Write qMark & today & qMark & coma
  f.Write qMark & username & qMark & coma
  f.Write qMark & computername & qMark & coma
  f.Write qMark & curTime & qMark & coma
  Set f = fso.OpenTextFile(filename, ForReading)
  WriteLineToFile = f.ReadAll

End Function


U.L Ranmal

IF not evaluating correctly

$
0
0

Hi,

I'm new to powershell, and I can't figure out why this simple IF is not detecting correctly. the function returns TRUE, however, the if will process the condition "-eq 9"... How is TRUE = 9?

It must be some technicality that I'm missing. When I move the "-eq 9" after the $true and $false evaluation, it works... without changing the order of the IF RESULT, how can I get it to process the correct evaluation. In the example below, it should process the code under "} elseif ($result -eq $true) {", not "if ($result -eq 9) {".

.

function myfunc ($param1) {
    if ($param1 -eq "a") {
        $result= $true
    } elseif ($param1 -eq "b") {
        $result= $false
    } else {
        $result= 9
    }   

    return $result
}

########
$myreturn = myfunc "a"
write-host "myfunc returned: $myreturn "

if ($myreturn -eq 9) {
    write-host "if detected: 9"
} elseif ($myreturn -eq $true) {
    write-host "if detected: true"
} elseif ($myreturn -eq $false) {
    write-host "if detected: false"    
}

.

.

thank you


Regular Expression not working as I would expect

$
0
0

Hi,

Maybe I am getting confused with regular expressions :

$Regex="'^([0-9a-f]{4}\.){2}([0-9a-f]{4})$'"

$Bill|Select-String$Regex-AllMatches

write-host $Matches

This returns no results !

$Bill contains the following :

Authentication successful for client (006e.0bfa.1gf4)

I don't understand why this regex doesn't match this value ?  I wonder is there any other way to match this type of value with the brackets ?  It will always be in the

format (xxxx.xxxx.xxxx)

Thanks

Bill

How To Set File Permissions

$
0
0

I just got out of Powershell training and I have set file/folder group and permissions on a lot of files?

I have a csv file which list file names and what group and permissions they should have. Now I need to process through and set the permissions. I used Icacls to set the owner and now I am trying to use set-acl to set the group and access permissions. I found the documentation from Google or Technet to be incomplete or not simple enough for me to understand.

Is there a web page with the steps on using set-acl? What is the best way to proceed (set-acl, Icacls, or some other way?

Thanks for any help?


Command succeeds in PowerShell console but fails when executed from ISE - what can it be?

$
0
0

I have a script that automates Node.js application deployment and in the process calls

`npm install`

This command works OK in both cmd.exe and powershell.exe but fails when executed from the script area of the ISE (tried to run it both under standard user and Administrator, the result is the same). The output is then something like:

npm : npm http GET https://github.com/agilio/node-sqlserver-compiled/tarball/master
At line:1 char:1+ npm install+ ~~~~~~~~~~~+ CategoryInfo          : NotSpecified: (npm http GET ht.../tarball/master:String) [], RemoteException+ FullyQualifiedErrorId : NativeCommandError
npm
http
GET
 https://github.com/bnoguchi/everyauth/tarball/express3
npm
http
GET
 https://github.com/agilio/loggly-console/tarball/master
npm
http
200
 https://github.com/agilio/node-sqlserver-compiled/tarball/master
npm
http
200
 https://github.com/agilio/loggly-console/tarball/master
npm
http
200
 https://github.com/bnoguchi/everyauth/tarball/express3
npm
WARN
excluding symbolic link
 test\app.js -> ../example/server.js
Maybe ISE blocks some network communication that the standard console doesn't? My setup is the default one in Windows 8 Pro.

Adding user to group cross domain

$
0
0
Hi, I want to do something every simple, but I can not find out what I am doing wrong.

setup is like this:

my forest plus 3 domains under it.

myforest.org
*domainAAA.myforest.org
*domainBBB.myforest.org
*domainCCC.myforest.org

I have a group in domainAAA called "BluePenUsers"

I want to add users from domainBBB and  domainCCC to this group

CODE: SELECT ALL
$gg = get-adgroup -filter {(displayname -eq "BluePenUsers")} -server domainAAA.myforest.org


CODE: SELECT ALL
$u1 = get-aduser"Username" -server domainAAA.myforest.ordg

CODE: SELECT ALL
$u2 = get-aduser"Username" -server domainBBB.myforest.ordg

CODE: SELECT ALL
$u3 = get-aduser"UserName" -server domainCCC.myforest.ordg



When I run the Add-ADGroupMember I get the following;

CODE: SELECT ALL
Add-ADGroupMember $gg $u1
Work!

CODE: SELECT ALL
Add-ADGroupMember $gg $u2
Fail! Add-ADGroupMember : The server is unwilling to process the request
CODE: SELECT ALL
Add-ADGroupMember $gg $u3
Fail! Add-ADGroupMember : The server is unwilling to process the request

Thanks for any help on this!!

Multiple regular expression matches ?

$
0
0

Hi,

I define the following variable :

$bill=Get-SqlData"Alpha02""testdb""select * from fail"|Where-Object{($_.Msgtext-like"*fail*")}

This give me the follwing :

MsgHostname : Alpha06

MsgText     : 13087: Dec  1 21:31:22: %DOT1X-5-FAIL: Authentication failed for client (16c7.r154.2g11) on Interface Gi0/13 AuditSessionID

The following regex is used to extract the value in between the ( ) :

($bill.Msgtext|Select-String-Pattern"\(.{4}\..{4}\..{4}\)"-AllMatches).Matches|Select-Expandvalue 

This works fine - but how can I then match up the MsgHostname value, i.e Alpha06 with the corresponding value from the regex ? i.e.

16c7.r154.2g11 Alpha06

Thanks

Bill

Convert Batch file with dir command to PowerShell

$
0
0

I am a noob at PowerShell and currently have a batch routine I run that I would like to convert into PowerShell. It checks a license server for the listing of current users for a particular application. Over time the licenses become orphaned and need to be removed from the applications license pool manually.

The current batch file is listed below:

------------------------------------------------

echo off

CLS

Echo On
dir \\192.10.0.23\SoftekData\SoftekLic /Q
pause

-----------------------------------------------

AND it's output is as follows:

----------------------------------------------

C:\Windows>dir \\192.10.0.23\SoftekData\SoftekLic /Q
 Volume in drive \\192.10.0.23\SoftekData has no label.
 Volume Serial Number is C5CB-3D6F

 Directory of \\192.10.0.23\SoftekData\SoftekLic

02/19/2013  02:40 PM    <DIR>          Computer\jsmith        .
02/19/2013  02:40 PM    <DIR>          BUILTIN\Administrators ..
02/19/2013  11:26 AM                 5 Computer\jgump        SFRAME.002
               1 File(s)              5 bytes
               2 Dir(s)  25,403,719,680 bytes free

C:\Windows>pause
Press any key to continue . . .

---------------------------------------------

Is anyone able to show me what the code equivalent in PowerShell would be so that I
can learn from it and take on converting similar batch files?

install

Retrieve list of files then use in options

$
0
0

I want to get a list of files that are in a directory then allow a user to select of the files to be deleted

select-string does not read context

$
0
0

I have the following code. The string outputs the correct row starting in "model name". However, I need the 2 rows following "model row" also. Where did I make the mistake?

Also, how do I properly output these results to a text file?

-Thanks

$a = get-content c:\temp\ext\*.ext -totalcount 30

$a | select-string -patter '(?<=model name)(.*)' -Context 0,2 | 
select -expa matches | select -expa value |  % { $_.trim() }




Script to put operating system version in AD computer description field

$
0
0

Hi,

In AD i cannot list the computers showing thier Operating System name like Windows 7 or Windows XP so its a pain to go through them all and separate them out . Can i run a script on the DC to insert the value from the Operating System objectclass into the description field within AD?

Thanks 

PowerShell script that will add a 2nd column with the distinguishedName attribute and save it as a new CSV file

$
0
0

Hello,

I have a CVS file that contain 4000 records with (DisplayName).

I need to find a PowerShell script that will add a 2nd  columnwith the distinguishedName attribute and save it as a new CSV file.

How can I do that?

Morris


Copy and Paste in Excel using powershell

$
0
0

Hi,

I have two excel file such as excel1.xls and excel2.xls. Now I am looking for a powershell script which will copy the contents in the cells A1 to A12 of excel1.xls and paste it to B2 to B13 cells of excel2.xls file.

Please help.

validate read-host to yes or no

$
0
0

I want to use read-host, but I only want the input to be yes or no.

How do I achieve this?

Trying to uninstall from an executable

$
0
0

Hey All,

So, today I was trying to uninstall SCCM 2012  client from a machine, and I wanted to use Powershell to do so. I've had a lot of dealings building and querying with Powershell, but I've never encountered a scenario where I had to call upon an external command. I've tried a few different things, but this is what I currently have:

$ccmuninstall=  Invoke-Expression "C:\Windows\ccmsetup\ccmsetup.exe /uninstall"
Invoke-Command -ComputerName win7computer01 {$ccmuninstall}

I tried using ampersand as well, a few different ways, but came up with nothing. Any ideas how or if this is even possible?

Thanks!


Viewing all 2562 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>