Questions tagged [powershell]

28216 questions
1

votes
2

answer
593

Views

How to replace first characters in a file name with a string?

I've been working on a script to maintain the archive from my IP camera DVR. My recording software outputs filenames formatted so that the first character is the camera number, followed by a date and time stamp. ex. 1_2017-11-03_00-45-07.avi I want to replace the first character with a string that r...
Mike Kent
1

votes
1

answer
5.6k

Views

Cannot index into a null array

I am using a template that sets the cell color based on a condition if a server backup was successful or not. I have the below code which is continuously throwing the error: Cannot index into a null array: Cannot index into a null array. At C:\Users\admin\Desktop\new.html.ps1:65 char:17 + ......
Vulgarito
1

votes
1

answer
733

Views

How can you delete files from an Azure WebApp using powershell?

Suppose I want to delete all files matching *.config.default from an Azure WebApp. How can I do this with PowerShell? If I had a directory on my local machine I can do this with: get-childitem -Path . -Filter *.config.default -Recurse | remove-item I can currently get the webApp using the followin...
WhiskerBiscuit
1

votes
3

answer
751

Views

faster way to get AD memberships for millions of AD groups in a network with multiple trusted forests and domains

First, I cannot get into why I need this data and I cannot get into specifics about the network. You'll have to trust me there is no other way to get this data other than a PowerShell script to run LDAP queries. I am working with a network that has multiple forests and multiple domains. There is a t...
IMTheNachoMan
1

votes
2

answer
1.9k

Views

Powershell for stopping and starting Azure VMs

I have created a 'Visual Studio Enterprise 2017 (latest release) on Windows Server (x64)' Virtual machine. I would like to start and stop this machine via Powershell. #Login Add-AzureAccount #Enterprize subscription. Id can be found by seraching for subsription in the portal Select-AzureSubscription...
Nigel Findlater
1

votes
1

answer
314

Views

Format-Table not displaying in function

I have a section of PowerShell code that reads a list of items from Azure, and formats them into a table for the user to choose from: if ($SubscriptionArray.Count -eq 1) { $SelectedSub = 1 } # Get SubscriptionID if one isn't provided while ($SelectedSub -gt $SubscriptionArray.Count -or $SelectedSub...
Bic
0

votes
0

answer
6

Views

Can we run an FME from a powershell?

I want to run an FME.fmw file from a PowerShell.ps1 one. I know that I can do that from a batch with the command 'fme C:\Path\of\fmw' but I do not find a way to do it with PowerShell. Start-Process will only open FME and not launch it $repertory_source = Read-Host 'Path of file ? (X:\X\X.fmw)' st...
Rhon Yz
1

votes
2

answer
131

Views

Dynamic DataTemplate in a PowerShell WPF app

I have created a simple WPF application using XAML and PowerShell, consisting of a TabControl in whose child TabItems I wish to display multiple kinds of data. Depending on the type of data provided, I want the TabControl's child TabItems to use a different DataTemplate. I understand that the best...
Spencer
1

votes
1

answer
392

Views

PowerShell Add-Content with multiple variables to columns

Using Add-Content to input multiple variables to a csv file will write each variable to the csv in a new row, such as the following: Code $name, $dateTime, $task | Add-Content $csv -Force Results $name $dateTime $task I would like to loop the above code with new variable values each time but would...
Garrett
0

votes
1

answer
26

Views

Change “Allow access to Azure services” settings of Azure SQL Server using powershell

I want to modify (enable/disable) 'Allow access to Azure services' settings of Azure SQL Server using powershell, based on a value from my JSON configuration I do have referred couple of StackOverflow, all the answers are to create a firewall rule '0.0.0.0' to enable and '255.255.255.255' to disable...
Manjunath Rao
0

votes
0

answer
5

Views

Powershell cannot use database view unless recompiled

Powershell program on Widnows server 2012 performs some database operations in Oracle database using regular Oracle client. Database is XE 11g. Program first retrieves data from a view and then loops through recordset and sends processed data to SQL server using its client. The problem is it sees so...
some1
1

votes
2

answer
44

Views

Handling 404 Errors in PowerShell

I have a huge list of blog URLs that I need to check the validity of. I've knocked together a script from this answer and from here. Here is my script: $siteURL = 'http://example.com/' $File = '.\urls.txt' $NewContent = Get-Content -Path $File | ForEach-Object { $_ $HTTP_Request = [System.Net.We...
Burgi
1

votes
2

answer
55

Views

Escaping command correctly for powershell.exe (launch command from CMD.exe)

How do I escape properly the below command? I need to run the powershell command from CMD because it's a stubpath value. The registry value for stubpath can't run powershell commands natively. The command is verified and works correctly. The problem is only to get it to run via powershell.exe -comma...
Rakha
1

votes
1

answer
34

Views

Exlude a row on Import-Csv

I have a question concerning Import-Csv in PowerShell. My script reads all *.csv files in my directory and writes it to my MS SQL database. So far so good, but I have a problem in these csv files. They all look the same like this: Header1;Header2;Header3 -----;-----;----- Data1;Data2;Data3 Data1;Da...
Patrick Sacher
1

votes
3

answer
35

Views

Powershell Remove Text Between before the file extension and an underscore

I have a few hundered PDF files that have text in their file names which need to be removed. Each of the file names have several underscores in their names depending on how long the file name is. My goal is to remove the text in that exists between the .pdf file extension and the last _. For exampl...
crazymatt
1

votes
2

answer
113

Views

How to loop through list of objects in terraform?

I am trying to dynamically fetch list of VMs from azure using external data source and display VM individually. Below is powershell script $rgroup = [Console]::In.ReadLine() $json = ConvertFrom-Json $rgroup $name = $json.rg $vm=Get-AzVM -ResourceGroupName $name | select name | convertTo-json Writ...
user2549572
1

votes
1

answer
50

Views

How to loop through 2 arrays in powershell?

I am fetching the disks along with the vm names to which those disk are attached, then i want save disk name & VM name in a variable as a json. when i run the below code i am not getting the desired output, can someone help me with the correct code. $disks=Get-AzDisk -ResourceGroupName MFA-RG | whe...
user2549572
1

votes
2

answer
26

Views

Remove row if term found in a CSV column

I need to remove a row from a table if I find a certain kind of email address in my CSV table. There are multiple email fields but I only want to do this for one email column, not any which is what I think the code below would currently do. How can I specify that? $data = foreach ($line in Get-Conte...
Dan
1

votes
2

answer
75

Views

How do I Compare Two Folders and Create Missing Subfolders in PowerShell

Hi folks, In PowerShell, I would like to compare two folders; [Folder1] and [Folder2], and for every folder missing in [Folder1], create it in [Folder2]. How do I do this? For example: > tree . ├───Folder1 │ ├───A0 │ ├───A1 │ ├───B0 │ └───B1...
Kismet Agbasi
1

votes
2

answer
63

Views

Converting a CSV file into appropriate XML format

I'm trying to get some tab-delimited CSV files into an XML via PowerShell, and I'm struggling to make sense of it all. I'm 100% sure it's something really simple I'm missing, so help would be massively appreciated. I'm very close but just can't get over the final hurdle. I essentially want this scri...
Honkytonk
1

votes
4

answer
76

Views

How to read text in .txt file and each of its value in powershell?

I have data in a text file like below, i want to read it value by value using powershell Name Enabled Description ---- ------- ----------- administrator1 True...
Tushar Raichand
1

votes
2

answer
69

Views

Powershell - Fastest way to create a very large array of hashtables

I am trying to create a rather large array of hashtables, with much of the data either fully randomized or randomly picked from a list. Here is my Current Code $ArrayData = @() $ArrayDataRows = 150000 foreach ($i in 1..$ArrayDataRows) { $thisobject = [PSCustomObject] @{ Number = $i Place = Get-Rando...
AdilZ
1

votes
1

answer
32

Views

error defining array in a script, it works if typed out however

I'm trying to create an audio toggle. I've written it before but new OS and dll updates forced me to rewrite it, the method i used previously doesn't seem to work any longer. So right out of the gate my first line spits out this error: $audio : The term '$audio' is not recognized as the name of a cm...
acme64
1

votes
2

answer
40

Views

Cast JSON to PowerShell with proper Auto-Completion (IntelliSense)

Is there a way to geth autocompletion with IntelliSense in Visual Studio Code work properly, when I read a json file and cast it to a PowerShell like: $config = Get-Content 'SOME_PATH' | ConvertFrom-Json $config.attribute1 The problem is that the file needs to be in memory before it can get the str...
nailuenlue
1

votes
1

answer
55

Views

Missing type name after '['

I'm trying to dynamically set the Type Name on a function: Currently this is the line I'm working on - $ace.AccessBits = ([MVPSI.JAMS.CredentialRights] $a.Value) What I want to be able to do is dynamically change the Type Name so have something like this: $typeName = 'MVPSI.JAMS.CredentialRights' $...
whoisearth
1

votes
1

answer
61

Views

Passing a variable which contains path to Get-ChildItem does not give result

I want to pass a path variable to Get-ChildItem. But, the path variable does not pick the path I tried the following [STRING]$global:svcName='RSCDsvc' $bsaPath=(Get-WmiObject -query 'select Pathname from win32_service where name='$svcName'').PathName write-output $bsaPath The above throws me the pat...
Linux Teche
1

votes
2

answer
35

Views

Using get-content to rename a folder?

I am attempting to rename a folder based on the first 10 characters inside a file using a powershell command. I got as far as far as pulling the data I need to use to rename but I don't know how to pass it. Get-Content 'C:\DATA\Company.dat' | Select-Object -first 10 | rename 'C:\DATA\FOLDER' 'C:\DAT...
astanix
1

votes
2

answer
48

Views

Powershell Get-Random with Constraints

I'm currently using the Get-Random function of Powershell to randomly pull a set number of rows from a csv. I need to create a constraint that says if one id is pulled, find the other ids that match it and pull their value. Here is what I currently have: $chosenOnes = Import-CSV C:\Temp\pk2.csv | s...
Bobby Zimmerman
1

votes
1

answer
22

Views

Reading corresponding lines from 2 text files with 2 foreach() loops

Needing to pull 2 objects from 2 different txt files, 1 getting the user id and the other the rule id. The line numbers should correlate to each other from both text files. The issue is the 2 foreach() loop, the outer one runs once and the inner one run for each rule id in the txt, problem is I dont...
mjzach22
1

votes
3

answer
1.3k

Views

C# Powershell Interop

The Sys Admin guy is writing some common housekeeping Power Shell scripts. Predominantly for AD management (updating exchange details, moving people around security groups etc.) I'd like to use these scripts from C# (I intend to write it as a library, consumed by a web site). I've seen this code pro...
Ed Blackburn
0

votes
0

answer
10

Views

Making Batch Code friendly for Powershell

I am trying to make the batch file code below friendly for powershell but I am not sure what syntax to use to make it work. Please assume that the variables have already been set. ruby '%FolderOne%\script.rb %FolderOne%\framework_properties.txt 2>&1 | tee -a %FolderOne%\Logs\%LOG_NAME% ' I put...
My Display Name
0

votes
0

answer
6

Views

Collating Variables to an Array

I wish to collate variables to an array. I have 10 variables directories, all with the same prefix, similar to dir1, dir2 etc. Can I simply assign all these to an array using a wildcard like so? $all_dirs = $dir*
inquisitive
1

votes
4

answer
3.7k

Views

Get todays appointments from outlook with powershell: Unwanted results

I'm extracting todays appointments with the following code: $olFolderCalendar = 9 $ol = New-Object -ComObject Outlook.Application $ns = $ol.GetNamespace('MAPI') $Start = (Get-Date).AddDays(-1).ToShortDateString() + ' 00:00' $End = (Get-Date).AddDays(+1).ToShortDateString() + ' 00:00' $Filter = '[Mes...
Fabian
0

votes
0

answer
15

Views

In Visual Studio Code, I am having trouble changing the folder that the power shell terminal runs out of

In VS Code, I cannot figure out how to change the folder that my terminal runs though. I have searched far and wide on the internet with no answer. Whenever I try to run my code, I get an error message saying, 'Attribute 'program does not exist ('C:\Projects/app.js')' and I am given the option to op...
RCE11
1

votes
0

answer
8

Views

how to run python 3 script in powershell

so I have my folder pinned to the sidebar and within that folder, I have my program I wish to run. Python is installed and I did make sure to add it to the PATH. But when I cd into my folder I get a response that no such file exists. I've uninstalled and reinstalled several times I've clicked the ad...
Silverback
1

votes
1

answer
20

Views

How to filter an object of devices to a single one based on hostname in powershell?

I'm trying to get the sitename of a device by using the hostname of a single device from an object that contains the details of 100's of devices in Powershell. How do I filter it? The data is originally from an API and is being pulled in as Json, I've converted it using ConvertFrom-Json so it shoul...
Weighsone
0

votes
0

answer
3

Views

Powershell GUI for various scripts to be executed

I have a list of Powershell scripts that perform various functions im trying to see how i can create a GUI using those scripts . Say like i have : Button 1 : Disk Cleanup Button 2 : Copy Files ( Should have a source and destination path being taken from Edit Box : meaning paths can be provided in...
Viswanath
1

votes
4

answer
1.4k

Views

Create Octopus machine via REST API (PowerShell)

I’m trying to create a machine via the REST API, calling it through PowerShell. According to the documentation, only the name and thumbprint of the machine are needed to create a machine. But everything I try leads to either 405 or 500 errors: $newMachine = @{Name='machineName';Thumbprint='asdvbls...
ShaneC
0

votes
1

answer
4

Views

Function to output multiple files with variable names

I have a folder full of files of various similar names. I am trying to use PowerShell to take each file and create a text file with the same name in a different location. I tried to run the following code, the output of $list shows all the files I intend to copy the names of. $list = Get-Child-It...
amatthew382
0

votes
1

answer
6

Views

Regex match does not produce desired result

This regex match $script:lstfilepath = ((Get-Content -path $script:ordfile) | Select-String -pattern '^file\s*=\s*(\\\\.*.lst)').matches.groups[1].value Produces this error on files that do not contain the match, leaving me to believe the return is null. Cannot index into a null array. At line:1 cha...
200mg

View additional questions