Optimization Solvers with Linear Constraints in R - nonlinear-optimization
Does anyone know of any R packages that allow you to maximize a function according to a linear constraint? I have been recommended ipoptr but I cannot figure out how to install it so that I can use the R interface.
For some background, I am trying to allocate 80M of investment (x) between 114 products in a way that maximizes revenue (y). Predicted revenue for each product and investment level is based off a two-part model i.e. it is the result of multiplying the predicted probability of any revenue being generated, by the predicted level of revenue, conditional on revenue being generated. Each line of the objective function represents the fitted model for each product.
I have tried using nloptr but it seems to be unable to find global maximums - it always gives me the same values as my starting values.
I am including the functions I am using below.
# Objective function
function(x){
return(-((plogis(-0.17283 + 0.0000471*x[1])*exp(9.53643)*(x[1])^0.468)+
(plogis(-0.17283 + 0.0000471*x[2])*exp(9.53643)*(x[2])^0.468)+
(plogis(-0.17283 + 0.0000471*x[3])*exp(9.53643)*(x[3])^0.468)+
(plogis(-0.17283 + 0.0000471*x[4])*exp(9.53643)*(x[4])^0.468)+
(plogis(-0.17283 + 0.0000471*x[5])*exp(9.53643)*(x[5])^0.468)+
(plogis(-0.17283 + 0.0000471*x[6])*exp(9.53643)*(x[6])^0.468)+
(plogis(-0.17283 + 0.0000471*x[7])*exp(9.53643)*(x[7])^0.468)+
(plogis(-0.17283 + 0.0000471*x[8])*exp(9.53643)*(x[8])^0.468)+
(plogis(-0.17283 + 0.0000471*x[9])*exp(9.53643)*(x[9])^0.468)+
(plogis(-0.17283 + 0.0000471*x[10])*exp(9.53643)*(x[10])^0.468)+
(plogis(-0.17283 + 0.0000471*x[11])*exp(9.53643)*(x[11])^0.468)+
(plogis(-0.17283 + 0.0000471*x[12])*exp(9.53643)*(x[12])^0.468)+
(plogis(-0.17283 + 0.0000471*x[13])*exp(9.53643)*(x[13])^0.468)+
(plogis(-0.17283 + 0.0000471*x[14])*exp(9.53643)*(x[14])^0.468)+
(plogis(-0.17283 + 0.0000471*x[15])*exp(9.53643)*(x[15])^0.468)+
(plogis(-0.17283 + 0.0000471*x[16])*exp(9.53643)*(x[16])^0.468)+
(plogis(-0.17283 + 0.0000471*x[17])*exp(9.53643)*(x[17])^0.468)+
(plogis(-0.17283 + 0.0000471*x[18])*exp(9.53643)*(x[18])^0.468)+
(plogis(-0.17283 + 0.0000471*x[19])*exp(9.53643)*(x[19])^0.468)+
(plogis(-0.17283 + 0.0000471*x[20])*exp(9.53643)*(x[20])^0.468)+
(plogis(-0.17283 + 0.0000471*x[21])*exp(9.53643)*(x[21])^0.468)+
(plogis(-0.17283 + 0.0000471*x[22])*exp(9.53643)*(x[22])^0.468)+
(plogis(-0.17283 + 0.0000471*x[23])*exp(9.53643)*(x[23])^0.468)+
(plogis(-0.17283 + 0.0000471*x[24])*exp(9.53643)*(x[24])^0.468)+
(plogis(-0.17283 + 0.0000471*x[25])*exp(9.53643)*(x[25])^0.468)+
(plogis(-0.17283 + 0.0000471*x[26])*exp(9.53643)*(x[26])^0.468)+
(plogis(-0.17283 + 0.0000471*x[27])*exp(9.53643)*(x[27])^0.468)+
(plogis(-0.17283 + 0.0000471*x[28])*exp(9.53643)*(x[28])^0.468)+
(plogis(-0.17283 + 0.0000471*x[29])*exp(9.53643)*(x[29])^0.468)+
(plogis(-0.17283 + 0.0000471*x[30])*exp(9.53643)*(x[30])^0.468)+
(plogis(-0.17283 + 0.0000471*x[31])*exp(9.53643)*(x[31])^0.468)+
(plogis(-0.17283 + 0.0000471*x[32])*exp(9.53643)*(x[32])^0.468)+
(plogis(-0.17283 + 0.0000471*x[33])*exp(9.53643)*(x[33])^0.468)+
(plogis(-0.17283 + 0.0000471*x[34])*exp(9.53643)*(x[34])^0.468)+
(plogis(-0.17283 + 0.0000471*x[35])*exp(9.53643)*(x[35])^0.468)+
(plogis(-0.17283 + 0.0000471*x[36])*exp(9.53643)*(x[36])^0.468)+
(plogis(-0.17283 + 0.0000471*x[37])*exp(9.53643)*(x[37])^0.468)+
(plogis(-0.17283 + 0.0000471*x[38])*exp(9.53643)*(x[38])^0.468)+
(plogis(0.11617 + 0.0000471*x[39])*exp(8.70643)*(x[39])^0.468)+
(plogis(0.11617 + 0.0000471*x[40])*exp(8.70643)*(x[40])^0.468)+
(plogis(0.11617 + 0.0000471*x[41])*exp(8.70643)*(x[41])^0.468)+
(plogis(0.11617 + 0.0000471*x[42])*exp(8.70643)*(x[42])^0.468)+
(plogis(0.11617 + 0.0000471*x[43])*exp(8.70643)*(x[43])^0.468)+
(plogis(0.11617 + 0.0000471*x[44])*exp(8.70643)*(x[44])^0.468)+
(plogis(0.11617 + 0.0000471*x[45])*exp(8.70643)*(x[45])^0.468)+
(plogis(0.11617 + 0.0000471*x[46])*exp(8.70643)*(x[46])^0.468)+
(plogis(0.11617 + 0.0000471*x[47])*exp(8.70643)*(x[47])^0.468)+
(plogis(0.11617 + 0.0000471*x[48])*exp(8.70643)*(x[48])^0.468)+
(plogis(0.11617 + 0.0000471*x[49])*exp(8.70643)*(x[49])^0.468)+
(plogis(0.11617 + 0.0000471*x[50])*exp(8.70643)*(x[50])^0.468)+
(plogis(0.11617 + 0.0000471*x[51])*exp(8.70643)*(x[51])^0.468)+
(plogis(0.11617 + 0.0000471*x[52])*exp(8.70643)*(x[52])^0.468)+
(plogis(0.11617 + 0.0000471*x[53])*exp(8.70643)*(x[53])^0.468)+
(plogis(0.11617 + 0.0000471*x[54])*exp(8.70643)*(x[54])^0.468)+
(plogis(0.11617 + 0.0000471*x[55])*exp(8.70643)*(x[55])^0.468)+
(plogis(0.11617 + 0.0000471*x[56])*exp(8.70643)*(x[56])^0.468)+
(plogis(0.11617 + 0.0000471*x[57])*exp(8.70643)*(x[57])^0.468)+
(plogis(0.11617 + 0.0000471*x[58])*exp(8.70643)*(x[58])^0.468)+
(plogis(0.11617 + 0.0000471*x[59])*exp(8.70643)*(x[59])^0.468)+
(plogis(0.11617 + 0.0000471*x[60])*exp(8.70643)*(x[60])^0.468)+
(plogis(0.11617 + 0.0000471*x[61])*exp(8.70643)*(x[61])^0.468)+
(plogis(0.11617 + 0.0000471*x[62])*exp(8.70643)*(x[62])^0.468)+
(plogis(0.11617 + 0.0000471*x[63])*exp(8.70643)*(x[63])^0.468)+
(plogis(0.11617 + 0.0000471*x[64])*exp(8.70643)*(x[64])^0.468)+
(plogis(0.11617 + 0.0000471*x[65])*exp(8.70643)*(x[65])^0.468)+
(plogis(0.11617 + 0.0000471*x[66])*exp(8.70643)*(x[66])^0.468)+
(plogis(0.11617 + 0.0000471*x[67])*exp(8.70643)*(x[67])^0.468)+
(plogis(0.11617 + 0.0000471*x[68])*exp(8.70643)*(x[68])^0.468)+
(plogis(0.11617 + 0.0000471*x[69])*exp(8.70643)*(x[69])^0.468)+
(plogis(0.11617 + 0.0000471*x[70])*exp(8.70643)*(x[70])^0.468)+
(plogis(0.11617 + 0.0000471*x[71])*exp(8.70643)*(x[71])^0.468)+
(plogis(0.11617 + 0.0000471*x[72])*exp(8.70643)*(x[72])^0.468)+
(plogis(0.90817 + 0.0000471*x[73])*exp(8.89943)*(x[73])^0.468)+
(plogis(0.90817 + 0.0000471*x[74])*exp(8.89943)*(x[74])^0.468)+
(plogis(0.90817 + 0.0000471*x[75])*exp(8.89943)*(x[75])^0.468)+
(plogis(0.90817 + 0.0000471*x[76])*exp(8.89943)*(x[76])^0.468)+
(plogis(0.90817 + 0.0000471*x[77])*exp(8.89943)*(x[77])^0.468)+
(plogis(0.90817 + 0.0000471*x[78])*exp(8.89943)*(x[78])^0.468)+
(plogis(0.90817 + 0.0000471*x[79])*exp(8.89943)*(x[79])^0.468)+
(plogis(0.90817 + 0.0000471*x[80])*exp(8.89943)*(x[80])^0.468)+
(plogis(0.90817 + 0.0000471*x[81])*exp(8.89943)*(x[81])^0.468)+
(plogis(0.90817 + 0.0000471*x[82])*exp(8.89943)*(x[82])^0.468)+
(plogis(0.90817 + 0.0000471*x[83])*exp(8.89943)*(x[83])^0.468)+
(plogis(0.90817 + 0.0000471*x[84])*exp(8.89943)*(x[84])^0.468)+
(plogis(0.90817 + 0.0000471*x[85])*exp(8.89943)*(x[85])^0.468)+
(plogis(0.90817 + 0.0000471*x[86])*exp(8.89943)*(x[86])^0.468)+
(plogis(0.90817 + 0.0000471*x[87])*exp(8.89943)*(x[87])^0.468)+
(plogis(0.90817 + 0.0000471*x[88])*exp(8.89943)*(x[88])^0.468)+
(plogis(0.90817 + 0.0000471*x[89])*exp(8.89943)*(x[89])^0.468)+
(plogis(0.90817 + 0.0000471*x[90])*exp(8.89943)*(x[90])^0.468)+
(plogis(0.90817 + 0.0000471*x[91])*exp(8.89943)*(x[91])^0.468)+
(plogis(0.90817 + 0.0000471*x[92])*exp(8.89943)*(x[92])^0.468)+
(plogis(0.90817 + 0.0000471*x[93])*exp(8.89943)*(x[93])^0.468)+
(plogis(0.90817 + 0.0000471*x[94])*exp(8.89943)*(x[94])^0.468)+
(plogis(0.90817 + 0.0000471*x[95])*exp(8.89943)*(x[95])^0.468)+
(plogis(0.90817 + 0.0000471*x[96])*exp(8.89943)*(x[96])^0.468)+
(plogis(0.90817 + 0.0000471*x[97])*exp(8.89943)*(x[97])^0.468)+
(plogis(0.90817 + 0.0000471*x[98])*exp(8.89943)*(x[98])^0.468)+
(plogis(0.90817 + 0.0000471*x[99])*exp(8.89943)*(x[99])^0.468)+
(plogis(0.90817 + 0.0000471*x[100])*exp(8.89943)*(x[100])^0.468)+
(plogis(0.90817 + 0.0000471*x[101])*exp(8.89943)*(x[101])^0.468)+
(plogis(0.90817 + 0.0000471*x[102])*exp(8.89943)*(x[102])^0.468)+
(plogis(0.90817 + 0.0000471*x[103])*exp(8.89943)*(x[103])^0.468)+
(plogis(0.90817 + 0.0000471*x[104])*exp(8.89943)*(x[104])^0.468)+
(plogis(0.90817 + 0.0000471*x[105])*exp(8.89943)*(x[105])^0.468)+
(plogis(0.90817 + 0.0000471*x[106])*exp(8.89943)*(x[106])^0.468)+
(plogis(0.90817 + 0.0000471*x[107])*exp(8.89943)*(x[107])^0.468)+
(plogis(0.90817 + 0.0000471*x[108])*exp(8.89943)*(x[108])^0.468)+
(plogis(0.90817 + 0.0000471*x[109])*exp(8.89943)*(x[109])^0.468)+
(plogis(0.90817 + 0.0000471*x[110])*exp(8.89943)*(x[110])^0.468)+
(plogis(-2.15083 + 0.0000471*x[111])*exp(9.87243)*(x[111])^0.468)+
(plogis(-2.15083 + 0.0000471*x[112])*exp(9.87243)*(x[112])^0.468)+
(plogis(-2.15083 + 0.0000471*x[113])*exp(9.87243)*(x[113])^0.468)+
(plogis(-2.15083 + 0.0000471*x[114])*exp(9.87243)*(x[114])^0.468)+
(plogis(-1.86183 + 0.0000471*x[115])*exp(9.04243)*(x[115])^0.468)+
(plogis(-1.86183 + 0.0000471*x[116])*exp(9.04243)*(x[116])^0.468)+
(plogis(-1.86183 + 0.0000471*x[117])*exp(9.04243)*(x[117])^0.468)+
(plogis(-1.86183 + 0.0000471*x[118])*exp(9.04243)*(x[118])^0.468)+
(plogis(-1.06983 + 0.0000471*x[119])*exp(9.23543)*(x[119])^0.468)+
(plogis(-1.06983 + 0.0000471*x[120])*exp(9.23543)*(x[120])^0.468)+
(plogis(-1.06983 + 0.0000471*x[121])*exp(9.23543)*(x[121])^0.468)+
(plogis(-1.06983 + 0.0000471*x[122])*exp(9.23543)*(x[122])^0.468)))
}
# Inequality constraint function
eval_g0_ineq <- function(x)
{
return(x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+x[7]+x[8]+x[9]+x[10]+x[11]+x[12]+x[13]+x[14]+x[15]+x[16]+x[17]+x[18]+x[19]+x[20]+x[21]+x[22]+x[23]+x[24]+x[25]+x[26]+x[27]+x[28]+x[29]+x[30]+x[31]+x[32]+x[33]+x[34]+x[35]+x[36]+x[37]+x[38]+x[39]+x[40]+x[41]+x[42]+x[43]+x[44]+x[45]+x[46]+x[47]+x[48]+x[49]+x[50]+x[51]+x[52]+x[53]+x[54]+x[55]+x[56]+x[57]+x[58]+x[59]+x[60]+x[61]+x[62]+x[63]+x[64]+x[65]+x[66]+x[67]+x[68]+x[69]+x[70]+x[71]+x[72]+x[73]+x[74]+x[75]+x[76]+x[77]+x[78]+x[79]+x[80]+x[81]+x[82]+x[83]+x[84]+x[85]+x[86]+x[87]+x[88]+x[89]+x[90]+x[91]+x[92]+x[93]+x[94]+x[95]+x[96]+x[97]+x[98]+x[99]+x[100]+x[101]+x[102]+x[103]+x[104]+x[105]+x[106]+x[107]+x[108]+x[109]+x[110]+x[111]+x[112]+x[113]+x[114]+x[115]+x[116]+x[117]+x[118]+x[119]+x[120]+x[121]+x[122]-80000)
}
# Lower bounds
lb = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# Upper bounds
ub = c(30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000),
Related
Cannot convert value to type "System.Int32"
I'm trying to import a csv file and select only columns where the number is above the a certain value (80): import-csv 'C:\temp\t\xxxx.csv' | where-object {[int]$_.utilization -gt 80} The error I'm getting is: Cannot convert value "17 %" to type "System.Int32". Error: "Input string was not in a correct format." At line:57 char:22 + import-csv 'C:\temp\t\xxxx.csv' | where-object {[int]$_.utilization -gt 80} + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger $_utilization seems to be an Object Any thoughts? I tried to convert with $util = [int]$_.utilization, but I get the following error: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32". At line:3 char:1 + $util = [int]$_.utilization + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
$_.utilization contains a string. To parse the numeric prefix as an integer, remove the space and percentage sign from the string first: [int]($_.utilization -replace ' %$') -gt 80
Update multiple users
I am trying to simultaneously update some users in my AD, but not being able to generate the script correctly. I am working with .csv and I get errors! script Import-Csv -Path C:\Temp\userteste3.csv | foreach-Object {Set-ADUser -Identity $_.SamAccountName -Add #{-telephoneNumber $_.telephoneNumber;-OtherPhone $_.OtherPhone;ipPhone $_.ipPhone}} csv error No linha:1 caractere:105 + ... oreach-Object {Set-ADUser -Identity $_.SamAccountName -Add #{-telepho ... + ~ O literal de hash estava incompleto. No linha:1 caractere:186 + ... ber $_.telephoneNumber;-OtherPhone $_.OtherPhone;ipPhone $_.ipPhone}} + ~ Token '}' inesperado na expressão ou instrução. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : IncompleteHashLiteral
Sending PowerShell output to a sub directory
I have a script that is generating multiple CSV files with the time stamp and group name as part of the file name. Right now the script outputs the CSV files into the directory the script is run from. I would like to have the CSV files output to a sub directory. Whenever I include a path in my Export-Csv command, I get an error. Here is what I have: #This array lists the names of the AD groups that the script will query to enumerate membership. $aGroups = #("Group1", "Group2") #Create a log file to verify that the script did not have any errors. $strTimestamp = [string](Get-Date -Format "yyyy-MM-dd_hh-mm") Start-Transcript ADGroupScriptLog-$strTimestamp.txt Write "Enumerating group membership. This may take some time..." #Loop through each group in the array and output to CSV each member in the group. foreach ($group in $aGroups) { Get-QADGroupMember $group -Indirect -Type 'user' -ShowProgress -ProgressThreshold 0 -Sizelimit '0' | Select-Object Name, SAMAccountName, givenName, sn, title, manager, Description | Export-Csv ($strTimestamp + "_" + $group + ".csv") -NoType } Write "Execution Complete" Get-Date -format s Stop-Transcript I have tried: Export-Csv \\Server\Share\File.csv ($strTimestamp + "_" + $group + ".csv") -NoType ********************** Windows PowerShell transcript start Start time: 20180207113151 Username : Machine : (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is OneSourceUsers-2018-02-07_11-31.txt Enumerating group membership. This may take some time... Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_OneSrce_BI_Browser.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_OneSrce_BI_Admin.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_OneSource MSSQL POC.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_One Source Reporting Alert.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_One Source Loads.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_One Sales Team.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "2018-02-07_11-31_OneSrce_BI_Contributor.csv" to type "System.Char". Error: "String must be exactly one character long." At E:\OneSource\get_OneSourceUsers.ps1:19 char:215 + ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand Execution Complete 2018-02-07T11:31:51 ********************** Windows PowerShell transcript end End time: 20180207113151 **********************
When you run something like ... | Export-Csv \\server\share($strTimestamp + "_" + $group + ".csv") -NoType PowerShell sees \\server\share as one argument, and ("something" + ".csv") as another argument (due to the grouping expression). The second positional parameter is passed to the parameter -Delimiter, which expects a single character. Hence the error you observed. To create a path string from several variables just use a string with nested variables: ... | Export-Csv "\\server\share\${strTimestamp}_${group}.csv" -NoType
What does this Powershell command mean in English?
In Exchange Management Shell, you can write a query to display administrator activities. Search-AdminAuditLog -Startdate ((get-date).AddDays(-5)) -EndDate (get-date) | where{$_.caller -ne "NT AUTHORITY\SYSTEM (MSExchangeHMWorker)"} |select Caller, Rundate, ObjectModified, CmdLetName, #{n="Parameters"; e={$e=$null;$_.CmdLetParameters|%{$e += ( " -" + $_.name.tostring() + " '" + $_.value + "'")};$e}} | ft -autosize Most of the commands make sense but can anyone explain the #... part in English? Specifically this part: #{n="Parameters"; e={$e=$null; $_.CmdLetParameters | %{$e += ( " -" + $_.name.tostring() + " '" + $_.value + "'")};$e}}
That's called a "Calculated property". It's a way to add a property in your selected object with the name specified by n= and having the value resulting from expression e=. http://technet.microsoft.com/en-us/library/ff730948.aspx
Convert accountExpires to DateTime in powershell commandline (last step, help needed)
I have the following piece of code that works fine to output the user's displayname and the accountExpires attribute from AD. I am running them on commandline because of restricted mode at work: $objSearch.findall() | %{" " + $_.properties.displayname + "| " + $_.properties.accountexpires} Now I need to convert this output, specifically the accountExpires attribute to a humanly readable date. After googling I figured that I can use something like the below to convert between the accountExpires and a datetime. [datetime]::fromfiletime(129138320987173880) But I am having issues combining the two. I tried the following: $objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires)} Cannot convert argument "0", with value: "System.DirectoryServices.ResultPropertyValueCollection", for "FromFileTime" to type "System.Int64": "Cannot convert the "System.DirectoryServices.ResultPropertyValueCollection" value of type "System.DirectoryServices.ResultPropertyValueCollection" to type "System.Int64"." At line:1 char:96 + $objSearch.findall() | %{" "+ $.properties.displayname + " " + [datetime]::fromfiletime <<<< ($.properties.accountexpires)} + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument $objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime $_.properties.accountexpires} Unexpected token '' in expression or statement. At line:1 char:99 + $objSearch.findall() | %{"This is "+ $.properties.displayname + " " + [datetime]::fromfiletime $_ <<<< .properties.accountexpires} + CategoryInfo : ParserError: (_:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken How can I convert the accountExpires to a humanly readable date?
You just miss that the underlying ADSI COM object presents properties as arrays here is a way to get the accountexpires property, just use $_.properties.accountexpires[0]. $search = [ADSISearcher]"OU=MonOu,DC=dom,DC=fr" $search.Filter = "(cn=Jean Paul Blanc)" $user = $search.FindOne() $user | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires[0])} This give for me : Jean Paul Blanc 12/07/2012 00:00:00