Have user select file name from commandline option menu - powershell

I want a script that asks the user to select from a list option, like:
Please select a kind of report
1.HeadOffice
2.All offices
3.Office1
4.Office2
5.Office3
Code:
$headoffice = headoffice.csv
$Alloffices = alloffices.csv
$Office1 = office1.csv
$Office2 = office2.csv
$Office3 = office3.csv
$csv = "selected one"
Foreach($target in $csv){
# Do custom scan AND rainbows.
}
Is this possible?

First, you should create a hashtable where the file names are numbered like so:
$table = #{
'1' = 'headoffice.csv'
'2' = 'alloffices.csv'
'3' = 'office1.csv'
'4' = 'office2.csv'
'5' = 'office3.csv'
}
Next, you can ask the user to select an option using Read-Host and a here-string:
$choice = Read-Host #'
Please select a kind of report
1.HeadOffice
2.All offices
3.Office1
4.Office2
5.Office3
'#
Finally, you can index the hashtable with that value and thereby access the appropriate file name:
$filename = $table[$choice]
Note that if you are working with CSV files, you might also want to read up on Import-Csv.

Related

powershell - get a keyword from query string

I'm learning PowerShell scripting & want to extract tableName from SQL Query String. For example, I've this query -
$q = "SELECT * FROM [TestDB].[dbo].Invoice_Details where Clientname='ABC'"
where I want to extract table name i.e. it should output this - Invoice_Details
Currently, I'm doing this with following working code -
$q1 = $q -split '\[dbo\]\.'
$q2 = $q1[1] -split ' where '
write-host $q2[0] #here I get it right (Invoice_Details)
But, sometimes the query may/ may not have bracketed names like - [TestDB].[dbo].
So, I want to optimize this code so that it will work even if query containing any combination of bracketed/ bracketless tableNames
Try something like this:
$res = ([regex]'(?is)\b(?:from|into|update)\s+(\[?.+\]?\.)?\[?(\[?\w+)\]?').Matches($q)
write-host $res[0].Groups[2].Value

using let blocks in orientdb select statements

Hopefully a quick one - I'm struggling to make a let statement work.
I have a database of People vertexes. The vertexes have ident fields and name fields. This query returns one row - a person named Bob.
select from Person where ident = 1
I want to return all rows with the same name as this person. There are two Bobs in the data (as proof, the following query returns two rows):
select from Person where name = 'Bob'
I think all of the following queries should return those same two rows, but they all return 0 rows. They all involve different ways of using a let statement. Can anyone see what I'm doing wrong?
select name from Person
let $tmp = (select from Person where ident = 1)
where name = $tmp.name
select name from Person
let $tmp = (select name from Person where ident = 1)
where name = $tmp
select name from Person
let $tmp = 'Bob'
where name = $tmp
$tmp will be a list of records, so you are asking to compare a string to a list, and it isn't working. You could do the following;
select name from Person
let $tmp = (select from Person where ident = 1)
where name = first($tmp).name
That wont return Person records though (only rows of names). Limiting the $tmp query will also improve the performance slightly. So the following is better;
select from Person
let $tmp = (select from Person where ident = 1 limit 1)
where name = first($tmp).name
But actually, using the let clause in this manner is not good. The docs say
The LET block contains the list of context variables to assign each
time a record is evaluated.
So it is* (*should be) better to rearrange your query entirely;
select expand($persons_with_name) let
$person_with_ident = first((select from Person where ident = 1 limit 1)),
$persons_with_name = (select from Person where name = $parent.$person_with_ident.name)
You can look if name is contained in $tmp
select name from Person
let $tmp = (select name from Person where ident = 1)
where name in $tmp
or if $tmp contains name
select name from Person
let $tmp = (select name from Person where ident = 1)
where $tmp contains name

Using "Select" in SharePoint list query

I have a list with a large number of datapoints. (130 columns, 31000 items). I run certain scripts to data-mine and/or update that info. One trick I use to speed up processing time is to pull the whole list into an array at the beginning. This allows powershell to query the array instead of going back to the list each time.
In these scripts I'm usually only searching a few of the fields, making it inefficient to pull all 130+ columns I'm hoping looking for a way to limit what fields are being pulled in.
In the below script, everything works fine until I add the "Select" part of the query. I then get an error of "Unable to index into an object of type System.Management.Automation.PSObject".
Any pointers are greatly appreciated!
$oList = $Web2.Lists["Forecasting data source"]
$aList = $Web.Lists["ArrayTest"]
filter MyFilter {if ($_["CHGTaskRegion"] -eq "Syracuse") {$_}}
$list = $oList.Items | MyFilter | Select ["CHGTaskFacility"],["CHGTicketNumber"]
$list | ForEach-Object {
$ListItem = $aList.Items.Add()
$ListItem["Title"] = [string]$_["CHGTaskFacility"]
$ListItem["Number"] = [string]$_["CHGTicketNumber"]
$ListItem.Update()
}
EDIT:
This is way after the fact but I saw this old post of mine and figured it was worth coming back to. The proper method isn't actually to pull in the whole list and then search it. The right way is to simply get just the items you wanted in the first place using a CAML query. Like so:
$spqQuery = New-Object Microsoft.SharePoint.SPQuery
$spqQuery.Query =
" <Where>
<Eq>
<FieldRef Name='CHGTaskRegion' />
<Value Type='File'>Syracuse</Value>
</Eq>
</Where>"
$spqQuery.ViewFields = "<FieldRef Name='CHGTaskFacility' /><FieldRef Name='CHGTicketNumber' />"
$spqQuery.ViewFieldsOnly = $true
$splListItems = $splList.GetItems($spqQuery)
Have you tried:
$list = $oList.Items | MyFilter | Select {$["CHGTaskFacility"]}, {$["CHGTicketNumber"]}
Add underscore after dollar signs. For some reason _ is not showing up in the code above.
/Pawel
I think the code should be :
$list = $oList.Items | MyFilter | Select CHGTaskFacility,CHGTicketNumber
The way you wrote it, means that you tried to type cast, anything between [ ] being the type.
And so it got confused.
CHGTaskFacility and CHGTicketNumber are properties of the $list of objects, and so Select CHGTaskFacility,CHGTicketNumber is sufficient for Powershell to select them.
UPDATE:
I think the filter code may be incorrect:
filter MyFilter {if ($_."CHGTaskRegion" -eq "Syracuse") {$_}}

Add data to csv column based on row value

I have a CSV that I read in. The problem is that this CSV as columns that are not filled with data. I need to go through a column and when the ID number matches the ID of the content I want to add, add in the data. Currently I have,
$counter = 0
foreach($invoice in $allInvoices){
$knownName += $info | where-Object{$_.'DOCNUMBR'.trim().contains($cleanInvNum)}
$DocNumbr = $info | where-Object{$_.'DOCNUMBR'.trim() -eq $cleanInvNum}
$output = $ResultsTable.Rows.Add(" ", " ", " ", " ", $DocNumbr[$counter].'ORG', $DocNumbr[$counter].'CURNT', $knownName[$counter].'DOCNUMBR', " ")
$counter++
}
The problem with this code is that it just adds rows under the CSV and does not add the data to the row. How can I do a statement where I find ID and add the above content into that row?
I was able to resolve my issue by reworking my foreach loop and setting the values directly like so,
$output.'CheckNumber' = $DocNumbr.'ORTRXAMT'
$output.'CheckDate' = $DocNumbr.'CURTRXAM'
$output.'Invoice Number' = $knownName.'DOCNUMBR'

how to convert count(*) and group by queries to yii and fetch data from it

I want to convert this query in yii
SELECT count(*) AS cnt, date(dt) FROM tbl_log where status=2 GROUP BY date(dt)
and fetch data from that. I try this command (dt is datetime field):
$criteria = new CDbCriteria();
$criteria->select = 'count(*) as cnt, date(dt)';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);
but no data will fetch!
wath can I do to get data?
Probably you see no data because you need assign data to model attributes which doesn't exist.
$criteria = new CDbCriteria();
$criteria->select = 'count(*) AS cnt, date(dt) AS dateVar';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);
This means that your model must have attributes cnt and dateVar in order to show your data. If you need custom query then check Hearaman's answer.
Try this below code
$logs = Yii::app()->db->createCommand()
->select('COUNT(*) as cnt')
->from('tbl_log') //Your Table name
->group('date')
->where('status=2') // Write your where condition here
->queryAll(); //Will get the all selected rows from table
Number of visitor are:
echo count($logs);
Apart from using cDbCriteria, to do the same check this link http://www.yiiframework.com/forum/index.php/topic/10662-count-on-a-findall-query/
If you use Yii2 and have a model based on table tbl_log, you can do it in model style like that:
$status = 2;
$result = Model::find()
->select('count(*) as cnt, date(dt)')
->groupBy('date(dt)')
->where('status = :status')
->params([':status' => $status ])
->all();